Spring security 知识笔记【入门】
一、生成spring boot项目文件
二、pom文件如下
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.Eleven</groupId> <artifactId>Eleven</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.M2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> </dependencies> </project>
二、Spring Boot启动文件
package Eleven; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan //扫描过滤器等servlet、filter注解 public class ElevenApplication { public static void main(String[] args){ SpringApplication.run(ElevenApplication.class,args); } }
三、新建两个controller
package Eleven.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class AuthenticationTestController { @GetMapping("/user") public String helloWorld(){ return "This is a user page!"; } @GetMapping("/admin/test/*") public String getAdminInfo(){ return "This is Admin page!"; } }
四、启动后,访问controller路径,提示需要登录,默认用户user,默认的密码是动态的,已在控制台输出
五、登录后可显示controller的内容
六、自定义用户名、密码
在配置文件(application.properties)中增加以下
spring.security.user.name = admin
spring.security.user.password = 123456