2017-06-09 22 views
0

私は認証され、Spring SecurityによってActive Directoryに承認されています。 しかし、LDAP属性(たとえば、MAIL)を取得することはできません。 私はそれ...InetOrgPersonContextMapperクラスの使用方法

@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; 
} 

ために使用InetOrgPersonContextMapperをしようとしているが、私は私のコントローラでretrive属性をしようとしたときには、ClassCastExeption

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    InetOrgPerson person = (InetOrgPerson)auth.getPrincipal(); 

が私にreitrive属性の正しい方法を教えてください取得します。

答えて

0

私はそれが良い方法ではないと思うが、それは働いている。 誰かがそれをより良くする方法を知っているなら、教えてください。

@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; 
     } 
    }; 
} 
関連する問題