Spring boot Spring Boot AntMatchers vs@PostAuthorize用法
我的任务是在我正在开发的RESTAPI中实现RBAC(基于角色的访问控制) 让我困惑的是,当我在扩展WebSecurityConfigureAdapter的安全类中使用configure方法antMatchers时,授权工作正常,但当我处理antMatchers并尝试在端点上用@PostAuthorize替换它们时,RBAC无法工作 这是我在扩展WebSecurity配置适配器的类中的配置方法:Spring boot Spring Boot AntMatchers vs@PostAuthorize用法,spring-boot,spring-security,rbac,Spring Boot,Spring Security,Rbac,我的任务是在我正在开发的RESTAPI中实现RBAC(基于角色的访问控制) 让我困惑的是,当我在扩展WebSecurityConfigureAdapter的安全类中使用configure方法antMatchers时,授权工作正常,但当我处理antMatchers并尝试在端点上用@PostAuthorize替换它们时,RBAC无法工作 这是我在扩展WebSecurity配置适配器的类中的配置方法: @Override protected void configure(HttpSecurity h
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.rememberMe()
.and()
.addFilter(new JwtUsernameAndPasswordAuthenticationFilter(authenticationManager()))
.addFilterAfter(new JwtTokenVerifierFilter(), JwtUsernameAndPasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.antMatchers("/user").authenticated()
.antMatchers("/hello").hasRole(ApplicationUserRole.ADMIN.name())
.anyRequest()
.authenticated();
http.headers().frameOptions().disable();/*REQUIRED FOR H2-CONSOLE*/
}
这很好用
这是在应该授权但没有授权的端点上的注释
@PostAuthorize("hasRole('ADMIN')")
@RequestMapping("/hello")
String hello(){
return "hello";
}
我做错了什么,它没有正常工作?您是否尝试用下面的注释来注释您的安全配置类 像这样的
@配置
@启用Web安全性
@EnableGlobalMethodSecurity(Prespenabled=true)
公共类SecurityConfig扩展了WebSecurity配置适配器{
受保护的void configure(最终HttpSecurity http)引发异常{}
}
要启用注释处理,您需要添加@EnableGlobalMethodSecurity