There might be many answers. One possible answer is to construct a base DN using the principal and query the directory server using a scope of base
, a filter '(&)'
and request the isMemberOf
attribute. For example, on my test system using a modern ldapsearch
command line tool and a principal of user.0
:
ldapsearch --hostname localhost --port 1389 \
--bindDN 'cn=directory manager' --baseDn \
'uid=user.0,ou=people,dc=example,dc=com' \
--searchScope base '(&)' isMemberOf
Password for user 'cn=directory manager':
dn: uid=user.0,ou=people,dc=example,dc=com
isMemberOf: cn=shadow entries,ou=groups,dc=example,dc=com
isMemberOf: cn=persons,ou=groups,dc=example,dc=com
This method requires knowledge of the namingContext
, in this case dc=example,dc=com
, and where the users are located in the tree. Another, similar method when the location of the user is not known would be be to first search for the user, then use the distinguished name from the search results to perform the above query. If the namingContext
is not known, it might be possible to discover the namingContext
from the root DSE. To recover the namingContext
from the root DSE, see this article.
There are some widely used directory servers that do not correctly support the LDAP standard and will reject the filter '(&)'
, if your directory server is one of these, simply substitute the presence filter '(objectClass=*)'
. There are many LDAP SDKs for Java, the one I prefer is the one from UnboundID.