DefaultLdapAuthoritiesPopulator の AuthoritiesMapper を作成しました。役割は正しくマッピングされており、intercept-url で機能しています。
getUserPrincipal() を使用してコントローラのロールにアクセスしようとすると、まだ LDAP グループを取得しています。
ポピュレータのコードを見て、うまくいくはずです。
何が間違っているか、またはどのように修正できるか考えていますか? 役割に応じてビューの一部を表示/非表示にする必要があります
/*************************************************************************************
* Maps Spring Security GrantedAuthorities
* e.g. AD groups populated using LdapAuthoritiesPopulator mapped to fixed role names
* as defined in a Map instance (e.g. populated from a property file)
*
* Sample roleMap:
* Key Value
* Group1 ROLE_USER
* Group2 ROLE_ADMIN
* Group3 ROLE_ADMIN,ROLE_USER
*************************************************************************************/
public class MapBasedGrantedAuthorityMapper implements GrantedAuthoritiesMapper {
private Map<String,String> roleMap;
private String stringSeparator = ",";
private SimpleGrantedAuthority unknownAuthorithy = new SimpleGrantedAuthority("ROLE_UNKNOWN");
private boolean keepUnknownAuthorities = false;
public MapBasedGrantedAuthorityMapper(Map<String,String> roleMap){
this.roleMap = roleMap;
}
public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) {
String[] mappedValues;
Set<GrantedAuthority> mapped = new HashSet<GrantedAuthority>(authorities.size());
for (GrantedAuthority auth : authorities) {
if (roleMap.containsKey(auth.getAuthority())) {
mappedValues = StringUtils.split(roleMap.get(auth.getAuthority()),stringSeparator);
for (String mappedValue: mappedValues) {
mapped.add(new SimpleGrantedAuthority(StringUtils.trimToEmpty(mappedValue)));
}
} else if (keepUnknownAuthorities){
mapped.add(auth);
} else if (unknownAuthorithy != null){
mapped.add(unknownAuthorithy);
}
}
return mapped;
}
// getters and setters
}