4

I'm using Spring LDAP 1.3.0 library to access an internal LDAP server, with Java, but I'm having troubles to do one thing: how can I get an internal attribute of any structure of LDAP? For example, how can I get the memberOf attribute of an user?

I ever searched a lot but don't find anything about that using Spring LDAP. Any ideas will be very welcome. Thanks.

2
  • I found a way and it's very simple. Example: getLdapTemplate().search( "dc=MY_COMPANY,dc=com,dc=br", "(&(objectClass=person)(uid=USER_UID))", SearchControls.SUBTREE_SCOPE, new String[]{ "memberOf" }, new UserAttributeMapper() ); Observation: in this code, the UserAttributeMapper is just my own implementation of org.springframework.ldap.core.AttributesMapper. Commented Jan 4, 2012 at 18:25
  • Possible duplicate of Why does Spring LDAP's LdapTemplate not return title, department & company attributes? Commented Mar 11, 2019 at 10:08

3 Answers 3

4

As you said in comment UserAttributeMapper is your friend !

If the user has more than one 'memberof' :

    static List<List<String>> getPersonGroupsByAccountName(String accountName){

    EqualsFilter filter = new EqualsFilter("sAMAccountName", accountName);
    return ldap.search(DistinguishedName.EMPTY_PATH,filter.encode(),new AttributesMapper(){
        public Object mapFromAttributes(
                javax.naming.directory.Attributes attrs)
        throws javax.naming.NamingException {
            List<String> memberof = new ArrayList();
            for (Enumeration vals = attrs.get("memberOf").getAll(); vals.hasMoreElements();) {
                memberof.add((String)vals.nextElement());
            }
            return memberof;
        }
    });

I'm sure there is a better way to do this but it works.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Of all the search results of all the posts this was the only one that worked for me. +1.
I’m glad it was helpful for you !
1

I use this to get fields like "createTimestamp" or "pwdChangedTime", and UserContextMapper you can reference resources: http://docs.spring.io/spring-ldap/docs/1.3.x-SNAPSHOT/reference/htmlsingle/

ldapTemplate.lookup(dn, new String[] {"*", "+"}, new UserContextMapper());

1 Comment

I would be curious about the referenced resource but unfortunately the link leads to a 404. Could you explain what the reference would show us?
0

It also works with odmManager. Something like

DistinguishedName dn = new DistinguishedName("The path your are searching in");
SearchControls searchControls = new SearchControls();
searchControls.setReturningObjFlag(true);
searchControls.setReturningAttributes("your attributes, as an array of strings");
return odmManager.findAll(User.class, dn, searchControls);

I use this to get fields like "createTimestamp" ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.