Search LDAP from Java

Category

Blog
24 December, 2014 0

This post explains how to connect to an LDAP server (in my case Apache DS) and retrieve elements that match a certain filter.

I have deployed an Apache Directory Server version 2.0 and imported the demo LDIF containing users and groups for the “sevenSeas” organization. You can download the file from the apache DS documentation.

This java code connects to the Apache DS deployed locally using the default port and user, and searches the context “ou=groups,o=sevenSeas” for groups the user “Fletcher Christian” belongs to.

import java.util.Properties;
 
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
 
public class LdapSearch {
public static void main(String[] args) throws NamingException {
InitialLdapContext ctx = constructInitialLdapContext();
// the name of the context to search
String contextName = "ou=groups,o=sevenSeas";
// Filter expression
String filterExpr = "(uniquemember={0})"; // selects the groups a user belongs to.
 
// Filter parameters (name of the user)
String userDN = "cn=Fletcher Christian,ou=people,o=sevenSeas";
Object[] filterArgs = { userDN };
 
SearchControls constraints = new javax.naming.directory.SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); // SUBTREE_SCOPE means recursive search
 
NamingEnumeration<SearchResult> search = ctx.search(contextName,
filterExpr, filterArgs, constraints);
while (search.hasMoreElements()) {
System.out.println(search.next().getName());
}
}
 
private static InitialLdapContext constructInitialLdapContext()
throws NamingException {
Properties env = new Properties();
env.put("java.naming.factory.initial",
"com.sun.jndi.ldap.LdapCtxFactory");
// LDAP url
env.put("java.naming.provider.url", "ldap://localhost:10389");
// ldap login
env.put("java.naming.security.principal", "uid=admin,ou=system");
env.put("java.naming.security.credentials", "secret");
 
return new InitialLdapContext(env, null);
}
 
}

With the demo LDIF imported in Apache DS the output will be:

cn=HMS Bounty,ou=crews
bkadmin
Wednesday December 24, 2014 - 21:12 Blog
Recent posts
Reclaim your disk space from Xcode

Do you have problems with the space on your disk? You are in the right place! This post is for […]

Read More…

Continue reading
25 June, 2020
RoR – Get your exceptions notified via Slack

Have you ever wanted to be one step ahead, and be notified of any unwanted exception before your users have […]

Read More…

Continue reading
18 October, 2018
Easy creation of a new Node + React project

Starting a new project is something everybody loves to do. However, some steps are always the same for every project. […]

Read More…

Continue reading
3 October, 2018
Learning SCRUM with LEGOS

In Kreitech, we know about Scrum, and we apply it in many of our projects, which gives us very positive […]

Read More…

Continue reading
19 September, 2018