依赖
<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>
启动类
@SpringBootApplication
public class SpringSecuriytDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSecuriytDemoApplication.class,args);
}
}
测试代码
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "test";
}
}
最简版示例,其实只要在springboot项目的基础上增加security的start依赖即可。效果是,我们所有的请求都需要先登录才能访问,登录页面是security自己提供的,不需要额外写。
测试使用
- 启动SpringSecuriytDemoApplication
- 控制台会打印"Using generated security password: xxxxx" ,xxxxx为spring security默认给的user用的密码
- 访问:http://localhost:8080/test 会自动跳转到登录页面,用户名输入user 密码输入控制台打印的密码登录后才能跳转到/test 请求地址。