0
votes

I tried to do LDAP authentication using spring security using below configuration. The authentication was successful.

   <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="" />

Now I need to get user details like domain ,organization etc from ldap itself,through java I am able to get the results using by calling search method of LdapContext. Is there a similar way to get the details required using spring security after successful authentication?

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

1 Answers

0
votes

You can use the userContextMapper property on the ldap authentication provider:

<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) {}

    }

You can then use ctx to query necessary information.