Although Java JNDI provides API for LDAP operations, but using JNDI normally need a lot of low-level programming work. Spring LDAP framework is a good way to do LDAP in Java. In this step-by-step guide, you will see how to connect to LDAP server and then search a user’s name using user’s ID.
Before you start, here is an open source ldap browser. By setting up correct login information, you should be able to browser the ldap directory. In this way, you have the idea what you can get from Ldap server.
1. We need both the Spring Framework binaries and the Spring LDAP binaries to run the following example. Spring LDAP requires J2SE 1.4 and is compatible with Spring Framework versions 1.2.8 and 2.0. The sample code in this article is based on the 1.1.2 version of Spring LDAP and has been tested using the Spring Framework 2.0.1.
- Download the Spring LDAP binaries from the Spring LDAP homepage.
- Download the Spring Framework 2.0.1 from the Spring Framework homepage.
2. Create the following classes. All the classes can be placed in one package directory, you add it to package in your way.
1. The ContactDAO interface
Define two functions. One is for getting all contact names, another is for getting contact details.
import java.util.List; public interface ContactDAO { public List getAllContactNames(); public List getContactDetails(String commonName); } |
2. LDAPContactDAO
Implement the interface defined.
import java.util.List; import javax.naming.NamingException; import javax.naming.directory.Attributes; import javax.naming.directory.BasicAttribute; import javax.naming.directory.BasicAttributes; import org.springframework.ldap.AttributesMapper; import org.springframework.ldap.LdapTemplate; import org.springframework.ldap.support.DistinguishedName; import org.springframework.ldap.support.filter.AndFilter; import org.springframework.ldap.support.filter.EqualsFilter; public class LDAPContactDAO implements ContactDAO{ private LdapTemplate ldapTemplate; public void setLdapTemplate(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } public List getAllContactNames() { return ldapTemplate.search("", "(objectClass=person)", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("mail").get(); } }); } public List getContactDetails(String objectclass){ AndFilter andFilter = new AndFilter(); andFilter.and(new EqualsFilter("objectClass",objectclass)); System.out.println("LDAP Query " + andFilter.encode()); return ldapTemplate.search("", andFilter.encode(),new ContactAttributeMapper()); } } |
3. springldap.xml
Here be careful about the base name, you can use a client to connect LDAP server first and see what is inside. I use OU= ***, becuase all our users are stored under this directory.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="contextSource" class="org.springframework.ldap.support.LdapContextSource"> <property name="url" value="ldap://your.ldap.url:389" /> <property name="base" value="base, be careful to put it right" /> <property name="userName" value="your username" /> <property name="password" value="password" /> </bean> <bean id="ldapTemplate" class="org.springframework.ldap.LdapTemplate"> <constructor-arg ref="contextSource" /> </bean> <bean id="ldapContact" class="com.javaworld.sample.LDAPContactDAO"> <property name="ldapTemplate" ref="ldapTemplate" /> </bean> </beans> |
4. ContactAttributeMapper
import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import org.springframework.ldap.AttributesMapper; public class ContactAttributeMapper implements AttributesMapper{ public Object mapFromAttributes(Attributes attributes) throws NamingException { ContactDTO contactDTO = new ContactDTO(); Attribute mail = attributes.get("mail"); Attribute sap = attributes.get("employeeNumber"); if(mail != null) contactDTO.setMail((String)mail.get()); if(sap != null) contactDTO.setSap((String)sap.get()); return contactDTO; } } |
5. ContactDTO
public class ContactDTO { String mail; String sap; public String getSap() { return sap; } public void setSap(String sap) { this.sap = sap; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String toString() { StringBuffer contactDTOStr = new StringBuffer("Person=["); contactDTOStr.append(" mail = " + mail); contactDTOStr.append(" ]"); return contactDTOStr.toString(); } } |
6. The testing class: SpringFrameworkLDAPClient
import java.util.List; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessException; public class SpringFrameworkLDAPClient { public static void main(String[] args) { //Resource resource = new ClassPathResource("/SpringLDAPClient/src/com/javaworld/sample/springldap.xml"); //System.out.println(resource.toString()); try { Resource resource = new ClassPathResource("springldap.xml"); BeanFactory factory = new XmlBeanFactory(resource); System.out.println(factory.toString() + "\n"); ContactDAO ldapContact = (LDAPContactDAO)factory.getBean("ldapContact"); List contactList = ldapContact.getContactDetails("30662"); //List contactList =ldapContact.getAllContactNames(); //System.out.println(contactList.size()); int count = 0; for( int i = 0 ; i < contactList.size(); i++){ System.out.print("Email: " + ((ContactDTO) contactList.get(i)).getMail() + " "); System.out.println("SAP: " + ((ContactDTO) contactList.get(i)).getSap()); count++; } System.out.println("\n" + count); } catch (DataAccessException e) { System.out.println("Error occured " + e.getCause()); } } } |
This tutorial is from Javaword, but I spent some time to make it really work.
Hello !
I used the same classes as you but i have one issue which is : I do research of Groups and i like to see the members. the mappers does not return a list of member but only the first one. How can I do to have all the members ? Otherwise, how can I get many values that hase the same attribute?
thanks.
class=”org.springframework.ldap.support.core.LdapContextSource” is
class=”org.springframework.ldap.core.support.LdapContextSource”
Hello, plz correct it. There’s a mistake
the class is wrong “core” package is missing
org.springframework.ldap.core.*
…
…
sincerly
Marco
Good article!
Take a notice with the version 1.3.2 some classes have been changed the package. For example
org.springframework.ldap.support.LdapContextSource which becomes
org.springframework.ldap.core.support.LdapContextSource.
Good Job.
Thanks for comment.
You are right. But here the program is for getting some user’s information from LDAP repository, the name & pw is for accessing the LDAP repository first.
I am newbie to LDAP – One question I have is why do you need to provide a pw to the the list of names. What I understand is lets say a user needs to be authenticated – the name & pw is supplied. I get the list of names from LDAP and search – if the credentials match then that user is authenticated. So what is the pw that is supplied to get the list of Names?
What version of Ldap are you trying to connect?
Hi,
Your article is quite easy to understand. thanks.
I have a question in the Ldap version. Do you know how we can set the Ldap version during the LdapBindRequest. I checked for the possibilities but i can not find a way to do it.
Thanks
Yes, the problem is here. It is uploaded now.
Thanks for letting me know this.
Hi there,
Thanks for this tutorial. It is a great one. However, we can one find the springLdap.xml file? Thank you