‘url‘ attribute is not specified and no embedded datasource could be configured & please sign in

一、Spring Initializr 快速创建Module

1.1 Spring Initializr 快速创建Module

在这里插入图片描述
选择需要的场景:

Spring Web Web
Thymeleaf  页面
Spring Security 安全
MyBatis Framework 访问数据库

在这里插入图片描述
在这里插入图片描述
创建HelloController:
在这里插入图片描述

HelloController

package com.djc.boot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JIACHENGER
 * @Description
 * @since 2023/7/18 15:17
 */
@RestController
public class HelloController {
    
    

    //响应浏览器发送的haha请求
    @GetMapping("/haha")
    public String hello() {
    
    
        return "Hello!2023718";

    }


}

1.2 Spring Initializer创建向导-一键创建好整个项目结构

在这里插入图片描述

二、‘url’ attribute is not specified and no embedded datasource could be configured

启动失败

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

在这里插入图片描述
原因:mybatis的依赖引入之后需要使用,如果暂时不使用就先关掉,否则就会报错。
解决:将与mybatis有关的依赖全部注释掉(然后点击Maven下方第一个的图标重新reload,Dependencies中的mybatis有关的依赖就会被去掉)
在这里插入图片描述
注释mybatis有关的依赖后的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.djc.boot</groupId>
    <artifactId>boot3-02-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot3-02-demo</name>
    <description>boot3-02-demo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <!--    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity6</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.2</version>
            <scope>test</scope>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

三、please sign in

3.1 Using generated security password: 7d2b9a84-e6b6-47b3-8b43-dddde85dbbde

在这里插入图片描述

3.2 please sign in

访问8080,浏览器中弹出please sign in窗口:
在这里插入图片描述
使用postman测试401无权限:
在这里插入图片描述
原因SpringSecurity的依赖 或 Spring Boot提供的安全自动配置类SecurityAutoConfiguration是(也就是说它自动集成了SpringSecurity)。

解决:取消SpringSecurity的依赖的自动配置。

spring启动类上加@SpringBootApplication(exclude={
    
    SecurityAutoConfiguration.class})  

spring启动类上加@SpringBootApplication(exclude={
    
    SecurityAutoConfiguration.class,SecurityFilterAutoConfiguration.class})

取消SpringSecurity的依赖的自动配置:

扫描二维码关注公众号,回复: 15915597 查看本文章
package com.djc.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;


/**
 * @author JIACHENGER
 */
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
public class Boot302DemoApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(Boot302DemoApplication.class, args);
    }

}

在这里插入图片描述

3.3 启动成功

在这里插入图片描述

3.3.1 whitelabel error page

访问http://localhost:8080/
在这里插入图片描述
原因:可忽略,因为没有写任何请求来处理。

package com.djc.boot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JIACHENGER
 * @Description
 * @since 2023/7/18 15:17
 */
@RestController
public class HelloController {
    
    

    //响应浏览器发送的haha请求
    @GetMapping("/haha")
    public String hello() {
    
    
        return "Hello!2023718";

    }


}

3.3.2 访问haha:http://localhost:8080/haha

在这里插入图片描述

3.3.3 postman访问haha请求

在这里插入图片描述

四、参考资料

Failed to determine a suitable driver class 的处理方法
springboot-莫名其妙的登录界面“Please sign in“
突如其来的“Please sign in”页面
Spring Boot安全自动配置
05快速入门:Spring Initializer

猜你喜欢

转载自blog.csdn.net/qyfx123456/article/details/131798396
今日推荐