SpringSecurity基本环境
环境搭建
- spring boot
- spring security
- 认证:判断用户是否是系统合法用户过程。
- 授权:判断系统内用户可以访问或具有访问哪些资源权限的过程。
创建项目
1.创建springboot应用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IrwuvMgw-1647417941388)(F:\笔记\SpringSecurity\screenshots\创建springboot项目.png)]
2.创建controller
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello security!");
return "hello security!";
}
}
3.启动项目进行测试
-http://localhost:8080/hello
整合 Spring Security
1.引入Spring Security相关依赖
<!-- 引入spring security依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.再次启动项目
(1)启动完成后控制台生成一个密码
(2)直接访问hello发现跳转到一个登录页面
3.登录系统
- 默认用户名为user
- 默认密码为 控制台打印的 UUID
这就是Spring Security的强大之处,只需要引入一个依赖,所有的接口就会自动保护起来!