java如何配置角色前缀Spring安全性
大家好。 试图使用不同的变体,但什么也没发生
我有一个代码,下一个方法是:
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获取所有用户权限,但只能使用前缀为“角色”的获取
我试过:
使用应用程序。yaml+代码(yaml中的前缀=null):
方法(AbstractContextSource ldapContext, AuthenticationManagerBuilder构建器)
builder.ldapAuthentication() .ldapAuthoritiesPopulator(populator) .contextSource(ldapContext) .userSearchFilter(userSearchFilter) .userSearchBase(userSearchBase) .groupSearchBase(groupSearchBase) .groupSearchFilter(groupSearchFilter) .rolePrefix(rolePrefix);
它不起作用
二,。不同的自定义类也不起作用
我的问题是如何获得所有没有前缀ROLE的角色
# 1 楼答案
如果使用
hasRole
进行授权,有两种方法:Javaconfig
XML配置
或者你可以用
hasAuthority()
代替hasRole()
。前一个在执行访问检查时不会添加PREFIX