配置前提
springboot2.4.1 与security
代码
@Override
protected void configure(HttpSecurity http) throws Exception {
//通过配置实现,不需要JWT令牌就可以访问的接口
for(String uri : jwtProperties.getPermitAllURI()){
http
.authorizeRequests().antMatchers(uri).permitAll();
}
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
// .antMatchers("/**").permitAll()
.anyRequest().authenticated()//任何没匹配上antMatchers的,只需要用户被验证有token的
// .anyRequest().access("@rabcService.hasPermission(request,authentication)")//可以放行经过验证有权限的用户
.and()
.cors()
.and()
.exceptionHandling()
.accessDeniedHandler(restfulAccessDeniedHandler())//没有权限时自定义异常
.authenticationEntryPoint(restAuthenticationEntryPoint());//没有token时自定义异常
http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
//RBAC权限控制级别的接口权限校验
// http
// .authorizeRequests().anyRequest()
// .access("@rabcService.hasPermission(request,authentication)");
}
最底下的注释掉了,后来放到了中间。也就是如下两行,只能有一行。
.anyRequest().authenticated()//任何没匹配上antMatchers的,只需要用户被验证有token的
// .anyRequest().access("@rabcService.hasPermission(request,authentication)")//可以放行经过验证有权限的用户
因为底层源码的关系,一旦使用了anyRequest(),就不能再使用第二次了,否则会报错
报错代码:Can’t configure anyRequest after itself