构建Spring Boot项目,添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
编写一个Controller
@RestController
public class HelloController {
@RequestMapping("hello")
public String hell() {
return "Hello World";
}
}
访问: http://localhost:8080/hello
会被重定向到:http://localhost:8080/login
Spring Security默认不允许访问,需要认证才能访问。
默认的用户名是:user
密码在控制台日志中
登录成功以后就会重定向到刚才访问的路径: http://localhost:8080/hello