0
在Spring Boot應用程序中已經定義了DefaultRolesPrefixPostProcessor,如參考手冊的8.3節所述。下面示出摘錄:Spring引導1.5.2.Release - DefaultRolesPrefixPostProcessor不會刪除默認的「ROLE_」前綴
public class DefaultRolesPrefixPostProcessor implements BeanPostProcessor, PriorityOrdered {
@Override
public Object postProcessAfterInitialization(final Object bean, final String beanName) {
if (bean instanceof DefaultMethodSecurityExpressionHandler) {
((DefaultMethodSecurityExpressionHandler) bean).setDefaultRolePrefix(null);
}
.... // if statement for DefaultWebSecurityExpressionHandler
.... // if statement for SecurityContextHolderAwareRequestFilter
return bean;
}
的SecurityConfig類替代配置(HttpSecurity)方法:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
....
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated();
http.addFilterBefore(authenticationFilter, BasicAuthenticationFilter.class);
}
}
然而任何@Secured方法仍需要 「ROLE_」 前綴,否則一個403被接收到,主要是由於默認的AccessDecisionManager的的RoleVoter返回0
@RestController
public class MyController {
@Secured("ROLE_XXX")
public String hello() {
return "hello";
}
}
反正是有解決這個問題,或者是使用「hasAuthority」只有這樣,才能避免這個問題?