简体   繁体   中英

Spring LdapTemplate - How do read values with same attribute name

I would like to read all groups of a user and put them into a list.

That's the ldap query:

ldapsearch -h [server_name] -p [port] -s sub -b "ou=people,ou=foo,o=bar,c=com" "(id=123)"

How the result looks like:

id: 123

name: John

firstname: BAKER

grouplist: cn=GROUP1.COM,cn=GROUPS,ou=FOO,o=BAR,c=COM

grouplist: cn=GROUP2.COM,cn=GROUPS,ou=FOO,o=BAR,c=COM

I use the LdapTemplate implementation of Spring. It works fine, but how can I handle results when they have multiple attributes with the same name (see above 'grouplist')? I tried to use the IncrementalAttributesMapper, but I didn't get it to work.

public MyUser getUser(String userId) {
   LdapQuery searchQuery = LdapQueryBuilder.query()
                .base("ou=people,ou=foo,o=bar,c=com")
                .searchScope(SearchScope.SUBTREE)
                .filter(new EqualsFilter("id", userId));

   return ldapTemplate.search(searchQuery, getUserAttributesMapper());
}

public static AttributesMapper getUserAttributesMapper() {
        return attributes -> {

            IncrementalAttributesMapper groupAttributesMapper = new DefaultIncrementalAttributesMapper("grouplist");

            MyUser myUser = MyUser.builder()
                 .id(attributes.get("id").get().toString())
                 .name(attributes.get("name").get().toString())
                 .firstname(attributes.get("firstname").get().toString())
                 .groupIds(groupAttributesMapper.getValues("cn"))
                 .build();

            return myUser;
        };
    }

When I try to read the attribute as follows:

attributes.get("grouplist").get().toString()

I get only the first group:

grouplist: cn=GROUP1.COM,cn=GROUPS,ou=FOO,o=BAR,c=COM

Thanks in advance for your support!

I solved the similar problem using this code:

List values = DefaultIncrementalAttributesMapper.lookupAttributeValues(ldapTemplate, baseDN, "member");

In that case I had group which contained a several members. Like this:

member: CN=Name1,OU=Users,DC=bar,DC=foo,DC=com
member: CN=Name2,OU=Users,DC=bar,DC=foo,DC=com
member: CN=Name3,OU=Users,DC=bar,DC=foo,DC=com

As a result I've got a List of separated members of group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM