Spring security
1.安全架构
1. 认证
who are you
登陆系统:用户系统
2. 授权
权限管理:用户授权
3. 攻击防护
- xss (cross-site scripting)
- csrf (cross-site request forgery)
- cors (cross-origin resource sharing)
- sql注入
4. 扩展:权限管理模型
a. RBAC(role based access controll)
- 用户(t_user)
- 用户角色关联表
- 角色(t.role)
- 角色权限关联表
- 权限 (t_permission)
b. ACL
- 直接用户和权限挂钩
用户(t.user)
权限(t_permission)
2.Spring security 原理
1.过滤器链架构

2.FilterChainProxy

3.SecurityFilterChain

3.使用
导入依赖
<!-- Spring Security依赖 -->
<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>
<scope>provided </scope>
</dependency>
配置类
@Configuration
public class AppSercuityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(registry ->{
registry.requestMatchers("/").permitAll() //允许所有人访问主页
.anyRequest().authenticated(); //剩下的所有请求都需要认证
});
//启用表单登陆
http.formLogin(FormLogin -> {
FormLogin.loginPage("/login").permitAll();
});
return http.build();
}
}
yaml配置文件中配置默认用户名和密码
spring:
security:
user:
name: user
password: 123
roles: admin,common,hr
不配置默认用户名:user
密码每次都是随机由控制台打印
自定义用户信息查询规则



开启方法级别的精确权限控制
配置类中 @EnableMethodSecurity
控制类中

