2017-06-09 3 views
0

Ich bin authentifiziert und berechtige zu Active Directory von Spring Security. LDAP-Attribute können jedoch nicht wiederhergestellt werden, z. B. MAIL. ich versucht, den Einsatz InetOrgPersonContextMapper für sie ...So verwenden Sie die InetOrgPersonContextMapper-Klasse

@Bean 
public InetOrgPersonContextMapper inetOrgPersonContextMapper(){ 
    InetOrgPersonContextMapper contextMapper = new InetOrgPersonContextMapper(); 
    return contextMapper; 
} 
@Bean 
public LdapAuthenticationProvider ldapAuthenticationProvider(){ 
    LdapAuthenticationProvider ldapAuthenticationProvider = new LdapAuthenticationProvider(ldapAuthenticator(),ldapAuthoritiesPopulator()); 
    ldapAuthenticationProvider.setUserDetailsContextMapper(inetOrgPersonContextMapper()); 
    return ldapAuthenticationProvider; 
} 

aber wenn ich retrive Attribute in Controller versuchen, i ClassCastExeption

Sie mich für reitrive Attribute richtige Art und Weise
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    InetOrgPerson person = (InetOrgPerson)auth.getPrincipal(); 

Bitte erzählen.

Antwort

0

Ich denke, es ist kein besserer Weg, aber es funktioniert. Wenn jemand weiß, wie es besser geht, bitte sag es mir.

@Bean 
public UserDetailsContextMapper userDetailsContextMapper(){ 
    return new LdapUserDetailsMapper(){ 
     @Override 
     public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) { 
      InetOrgPersonContextMapper personContextMapper = new InetOrgPersonContextMapper(); 
      UserDetails cm = personContextMapper.mapUserFromContext(ctx,username,authorities); 
      String MAIL = ((InetOrgPerson)(personContextMapper.mapUserFromContext(ctx,username,authorities))).getMail(); 
      String FullName = ((InetOrgPerson)(personContextMapper.mapUserFromContext(ctx,username,authorities))).getDisplayName(); 
      System.out.println("MAIL: " + MAIL + " Full Name: " + FullName); 
      return cm; 
     } 
    }; 
}