Shivayan Mukherjee
Shivayan Mukherjee

Reputation: 827

How to find all groups in LDAP using Spring which falls under the same CN?

I have this requirement where I need to find all groups in LDAP which comes under the same common name(CN). For example, I am able to find the definition for one using the below piece of code.

("CN=developer.eastRegion,OU=developerGroups,DC=mycomp,DC=com")

DirContextOperations lookupContext =
          ldapTemplate.lookupContext("cn=developer.eastRegion,ou=developerGroups,dc=mycomp,dc=com");

The actual task is to do a wild search for common names and list all groups, ("CN=developer.*,OU=developerGroups,DC=mycomp,DC=com")

Any insight would be very helpful. Thanks in advance.

Upvotes: -1

Views: 113

Answers (1)

grawity_u1686
grawity_u1686

Reputation: 16572

Specify ou=Groups,dc=kp,dc=org as your search base; use the LDAP filter to match the remaining attribute, e.g. a basic (cn=developer.*) or a more precise (&(objectClass=group)(cn=developer.*)).

(The leftmost RDN of an entry's DN belongs to the entry itself, so it can always be matched using a filter. All other RDNs generally cannot be searched for, at least not in Active Directory, though it's possible in OpenLDAP.)

Purely based on looking at docs and other SO answers, it might be something like this:

var controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

var results = ldapTemplate.search("ou=Groups,dc=kp,dc=org",
                                  "(cn=developer.*)",
                                  controls);

Make sure you're using SUBTREE as the search scope. It's usually the default, but it seems it may need to be specified through SearchControls.SUBTREE_SCOPE.

Upvotes: 1

Related Questions