Spring Web Web
Thymeleaf 页面
Spring Security 安全
MyBatis Framework 访问数据库
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";
}
}
启动失败:
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>
访问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的依赖的自动配置:
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);
}
}
访问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";
}
}
Failed to determine a suitable driver class 的处理方法
springboot-莫名其妙的登录界面“Please sign in“
突如其来的“Please sign in”页面
Spring Boot安全自动配置
05快速入门:Spring Initializer