美好的一天同事。尝试使用不同的变体但没有发生任何事情。
我有一个代码,其中是下一个方法:
public List<String> getUserRoles() {
List<String> roles = new ArrayList<>();
for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
roles.add(authority.getAuthority().toUpperCase());
}
return roles;
}
我想从Active Directory获取所有用户身份验证,但只能使用前缀“ROLE_”
我试过了:
builder.ldapAuthentication()
.ldapAuthoritiesPopulator(populator)
.contextSource(ldapContext)
.userSearchFilter(userSearchFilter)
.userSearchBase(userSearchBase)
.groupSearchBase(groupSearchBase)
.groupSearchFilter(groupSearchFilter)
.rolePrefix(rolePrefix);
它不起作用。2.不同的自定义类也不起作用。
我的问题是如何获得没有前缀ROLE_的所有角色。
如果您使用hasRole
进行授权,这里有两种方法:
Javaconfig
@Bean
public DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler() {
DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
defaultWebSecurityExpressionHandler.setDefaultRolePrefix("");
return defaultWebSecurityExpressionHandler;
}
XML配置
<beans:bean id="expressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler">
<beans:property name="defaultRolePrefix" value="" />
</beans:bean>
或者你可以使用hasAuthority()
而不是hasRole()
。进行检查时,前一个不会添加PREFIX
。