赞
踩
Spring Security作为安全框架的核心功能:
认证:验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户
授权:经过认证后判断当前用户是否有权限进行某个操作
properties
包下的ConfigPropertites
类。用来将配置文件中的属性映射到实体上
@ConfigurationProperties(prefix = "buxiwan")
@Component
@Data
public class ConfigProperties {
private String[] nonFilterPath; //定义了不需要经过过滤器的路径。
private String[] jwtFilterPath; //定义了需要经过JWT过滤器的路径。
private String[] staticSource; //定义了静态资源路径。
//定义了访问令牌和刷新令牌的过期时间。
private long accessTokenExpiration;
private long refreshTokenExpiration;
}
使用该类结合SecurityConfig
,可以快速设置需要鉴权的接口。
config.permission.expression
包下RbacExpressionRoot
类基于角色的自定义鉴权 鉴权时调用
@Component("rbacExpressionRoot") public class RbacExpressionRoot { public boolean hasAuthority(String authority) { //获取当前用户的权限 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { throw new BizException(ErrorCode.NO_SIGN_IN); } Object loginUser = authentication.getPrincipal(); if (loginUser instanceof UserPermission) { return ((UserPermission) loginUser).getAuthorities().contains(authority); } else { return false; } } public boolean hasRole(String role) { //获取当前用户角色 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { throw new BizException(ErrorCode.NO_SIGN_IN); } Object loginUser = authentication.getPrincipal(); if (loginUser instanceof UserPermission) { return ((UserPermission) loginUser).getRoles().contains(role); } else { return false; } } }
config
包下的SecurityConfig
类Spring Security配置
@Configuration //启用全局方法安全性,其中prePostEnabled = true表示启用@PreAuthorize和@PostAuthorize注解。 @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter; //JwtAuthenticationTokenFilter放在所有过滤器前面,检查浏览器携带的token是否合法 @Autowired AccessDeniedHandler accessDeniedHandler; @Autowired AuthenticationEntryPointImpl authenticationEntryPoint; //用户未登录时访问需要jwt接口 @Autowired ConfigProperties configProperties; @Override protected void configure(HttpSecurity http) throws Exception { http //允许跨域 .cors().and() //关闭csrf 前后端分离项目不必担心csrf攻击 .csrf().disable() //不通过Session获取SecurityContext。 设置为无状态的Session管理,因为项目采用JWT,不需要HttpSession。 .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() //开始定义请求权限。 // 允许匿名访问的接口(只允许未登录时访问) .antMatchers(configProperties.getNonFilterPath()).anonymous() //静态资源(swagger)放行 .antMatchers(configProperties.getStaticSource()).permitAll() //基于配置为某个接口设置权限。使用到之前的configProperties,rbacExpressionRoot。 //对于指定路径 configProperties.getJwtFilterPath(),只有具有 'user' 角色的用户才能访问。 .antMatchers(configProperties.getJwtFilterPath()).access("@rbacExpressionRoot.hasRole('user')") // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated(); //添加过滤器。在默认的UsernamePasswordAuthenticationFilter之前添加自定义的JWT过滤器。 http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); //配置自定义异常处理器。设置自定义的认证入口点和访问被拒绝的处理器。 http.exceptionHandling() .authenticationEntryPoint(authenticationEntryPoint) .accessDeniedHandler(accessDeniedHandler); } //暴露AuthenticationManagerBean @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } //自定义密码encoder @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
RBAC(Role-Based Access Control)
,基于角色的访问控制。通过用户关联角色,角色关联权限,来间接的为用户赋予权限。
RBAC0
模型最简单的用户、角色、权限模型。包含两种:
- 用户和角色是多对一关系。
- 用户和角色是多对多关系。
适合:系统功能单一,使用人员较少,岗位权限清晰,不会出现兼岗。
RBAC1
模型RBAC0
加上 角色继承。
子角色可以继承父角色的所有权限。
适合:有权限继承关系。如经理、主管、专员。主管的权限不能大于经理,专员的权限不能大于主管。
RBAC2
模型RBAC0
加上 对角色的一些限制:角色互斥、基数约束、先决条件角色、运行时互斥。
角色互斥:同一用户不能分配到一组互斥角色(权限互相制约)集合中的多个角色。
基数约束:一个角色被分配的用户数量有限制。
先决条件角色:要想获得较高的权限,首先拥有低一级的权限。
运行时互斥:允许一个用户具有两个角色,但运行中不可同时激活这两个角色。
RBAC3
模型称为统一模型,综合了 RBAC0
、RBAC1
和 RBAC2
的所有特点。
“用户组”,就是将相同属性的用户归类到一起。
数量有限制。
先决条件角色:要想获得较高的权限,首先拥有低一级的权限。
运行时互斥:允许一个用户具有两个角色,但运行中不可同时激活这两个角色。
RBAC3
模型称为统一模型,综合了 RBAC0
、RBAC1
和 RBAC2
的所有特点。
“用户组”,就是将相同属性的用户归类到一起。
例如公司有多个部门,每个部门中的用户权限相同。这样分配权限就能以部门为单位,方便管理,减少工作量。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。