- 使用Idea的Spring Initializr或者SpringBoot官网下载quickstart
添加依赖
1
2
3
4<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>新建控制器
1
2
3
4
5
6
7
8
9import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user")
public String getUsers() {
return "Hello Spring Security";
}
}logback.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file> 大专栏 Java-Springboot-集成spring-security简单示例(Version-springboot-2-1-3-RELEASE/data/www/file/logs/springboot.log</file>
<encoder>
<pattern>%date %d{HH: mm:ss.SSS} %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date %d{HH: mm:ss.SSS} %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>application.properties
1
2
3# Server Domain-Port
server.address=127.0.0.1
server.port=9090启动SpringBootApplication,springboot已经和spring-security集成了,如果直接访问
http://localhost:9090/user
会跳到登陆页面,这是spring-security自带的,但是我们并没有创建任何用户啊,spring-security有个默认的用户名user
,密码在控制台默认密码在控制信息里,在控制台信息里搜索
Using generated
,当然你的程序生成的密码肯定和我的不一样1
Using generated security password: 6ae529ee-2281-4b66-8f30-b1ba0e7fec97
使用用户名和密码登陆后: