2016-04-19 202 views
0

我尝试使用以下配置使用spring安全性进行LDAP身份验证。身份验证成功。Spring Security + Ldap身份验证

<authentication-manager> 
    <ldap-authentication-provider 
     user-search-filter="(uid={0})" 
     user-search-base="ou=people" 
     > 
    </ldap-authentication-provider> 
    </authentication-manager> 

    <ldap-server url="ldap://ldap.XXX.net/dc=XXX,dc=com" manager-dn="dc=XXX,dc=com" manager-password="" /> 

现在我需要获得用户的详细信息,如域,组织等从LDAP本身,通过Java我可以使用通过调用的LdapContext的搜索方法得到的结果。是否有类似的方式获得成功验证后使用spring安全性所需的详细信息?

LdapContext ctx = null; 
    NamingEnumeration<SearchResult> results = null; 
    results = ctx.search(baseDn,searchFilter,controls); 

回答

0

您可以使用LDAP身份验证提供了userContextMapper属性:

<authentication-manager> 
     <ldap-authentication-provider 
      user-search-filter="(uid={0})" 
      user-search-base="ou=people" 
      user-context-mapper-ref="customUserContextMapper" /> 
      > 
     </ldap-authentication-provider> 
     </authentication-manager> 


    public class CustomUserContextMapper extends LdapUserDetailsMapper { 
     @Override 
     public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {} 

    } 

然后可以使用ctx来查询需要的信息。

+0

谢谢迈克尔。有效。 – Nagendra

相关问题