// @Override
// public void configure(WebSecurity web) throws Exception {
// web.expressionHandler(new DefaultWebSecurityExpressionHandler(){
// private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
// @Override
// protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {
// WebSecurityExpressionRoot root = new WebSecurityExpressionRoot(authentication, fi);
// root.setDefaultRolePrefix("");
// return root;
// }
// });
// }
// @Bean
// GrantedAuthorityDefaults grantedAuthorityDefaults() {
// return new GrantedAuthorityDefaults(""); // Remove the ROLE_ prefix
// }
尝试的两种方法都无效
你的意思是Security自动给你的角色加上前缀的步骤,你想去掉吧
你现在的代码运行时获取
GrantedAuthority
的时候是有ROLE_前缀的?可能是你在add roles的时候加上了
spring security的org.springframework.security.core.userdetails.User#roles方法里面有一段硬编码
你看看是不是调用了这个方法.如果调用了,不要用这个方法,改为使用#authorities这个方法
再补充一下,spring security的角色如果不要前缀的话,可能会带来一些麻烦,比如多维度鉴权的时候,就好像表达式和角色同时鉴权,你需要一个办法区分表达式和角色