//注解方式
@PreAuthorize("hasAuthority('test')")
public RespBean test(){
....
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login","/logout").hasAuthority("test");
}
在过滤器链中FilterSecurityInterceptor负责权限的效验,所以从这个过滤器来展开
①对角色权限进行设置
通过阅读源码发现FilterSecurityInterceptord通过调用SecuritymetadataSource子类对象的getAttributes方法拿到Collection 以上例子:通过请求路径去数据库中查询对应的角色权限,得到角色权限。通过SecurityConfig.createList(str)方法对得到的角色权限进行设置。其中SecurityConfig是上述ConfigAttribute的子类,其中createList方法对权限的信息进行了设置。然后又通过getAttribute进行获取。
在FilterSecurityInterceptord中也对角色权限判断的方法进行调用,通过invoke方法调用父类AbstractSecurityInterceptor,然后父类通过调用accessDecisionManager.decide(authenticated, object, attributes)方法作出决定,是否符合权限的信息。我们可以通过实现实现AccessDecisionManager接口,重写decide方法,自定义其中的权限规则。
对于FilterSecurityInterceptord是自动调用默认规则的,所以我们需要对这个规则进行改变,使用我们的规则。
@Component
public class CustomFilter implements FilterInvocationSecuritymetadataSource {
@Autowired
private IMenuService menuService;
AntPathMatcher antPathMatcher=new AntPathMatcher();
@Override
public Collection
//createList
public static List
②自定义角色权限的判断
@Component
public class CustomUrlDecisionManager implements AccessDecisionManager {
@Override
public void decide(Authentication authentication, Object o, Collection
③最后对我们自定义的SecuritymetadataSource对象和AccessDecisionManager对象进行设置。
@Override
protected void configure(HttpSecurity http) throws Exception {
//使用jwt不需要csrf
http.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// .antMatchers("/login","/logout")
// .permitAll()
//除上面,所有请求都需要认证
.anyRequest()
.authenticated()
//设置自定义规则
.withObjectPostProcessor(new ObjectPostProcessor
流程图,做的不好请见谅



