Reputation: 1482
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.
Upvotes: 4
Views: 8498
Reputation: 11
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());
Upvotes: 1
Reputation: 808
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.
Upvotes: 4
Reputation: 3156
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" ....
Upvotes: 0