最近想要把SpringSecurity巩固一下,按黑马的视频学习,SpringSecurity黑马程序员全套教程,从入门到精通(IDEA版)超完整教程,手把手教学_哔哩哔哩_bilibili
一、创建工程并导入依赖
先搭建springboot工程
-
pom.xml
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
-
controller
@RestController
@RequestMapping("product")
public class ProductController {
@GetMapping("/test")
public String test(){
return "sucess";
}
}
二、加入SpringSecurity
1.引入security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.启动服务
- 访问后端服务,发现跳转到了登录页面
- 这是因为SpringSecurity默认实现是拦截器链,对我们进行了拦截,需要我们登录,登录名默认是 user,密码是自己启动后台服务时候控制台打印的。
- 登陆成功