Sunday, May 18, 2008

Spring LDAP

When we're writing the intranet web application we should implement some authentification mechanism. There are a lot of ways to do it, but a more conveniently just to use a Spring LDAP framework. Here are the steps to set up this structure.
1. Download Spring LDAP form the site:
2. Create Java Project;
2.1 Create package com.ldap.examples;
3. Import to it following jars:
i. commons-lang.jar
ii. commons-logging.jar
iii. commons-pool.jar
iv. ldapbp.jar
v. spring-beans.jar
vi. spring-context.jar
vii. spring-core.jar
viii. spring-dao.jar
ix. spring-jdbc.jar
x. spring-ldap-1.2.1.jar
4. Create PersonDao interface:

package com.ldap.examples;
import java.util.List;
public interface PersonDao {
public List getAllContactNames();
public List getContactDetails(String commonName,String lastName);
public List getPersonMail(String mail);

5. Create Java's class that implemets PersonDao interface

package com.ldap.examples;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
public class PersonDaoImpl implements PersonDao {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
* @param args
public static void main(String[] args) {
Resource resource = new ClassPathResource("com/ldap/examples/springldap.xml");
BeanFactory factory = new XmlBeanFactory(resource);
LdapTemplate ldapContact = (LdapTemplate) factory.getBean("ldapTemplate");
PersonDaoImpl my = new PersonDaoImpl();
public List getAllContactNames() {
// TODO Auto-generated method stub
return null;
public List getPersonMail(String mail) {
AndFilter andFilter = new AndFilter();
andFilter.and(new EqualsFilter("objectclass","person"));
andFilter.and(new EqualsFilter("mail",mail));
System.out.println("LDAP Query " + andFilter.encode());
return"", andFilter.encode(),new ContactAttributeMapper());
public List getContactDetails(String commonName, String lastName) {
// TODO Auto-generated method stub
return null;

6. Create springldap.xml
-??xml version="1.0" encoding="UTF-8"?>
-??!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" -"">
-?bean id="contextSource"
-?property name="url" value="ldap://ldapServerIPorName:389" />
-?property name="base" value="" />
-?bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
-?constructor-arg ref="contextSource" />
-?bean id="ldapContact"
-?property name="ldapTemplate" ref="ldapTemplate" />
Change ? to the < (Blogger bug???)
Put this file into package.
7. Create ContactDTO java's class

package com.ldap.examples;
public class ContactDTO {
String commonName;
String lastName;
String description;
String mail;
public String getCommonName() {
return commonName;
public void setCommonName(String commonName) {
this.commonName = commonName;
public String getDescription() {
return description;
public void setDescription(String description) {
this.description = description;
public String getLastName() {
System.out.println("Last name: " + lastName);
return lastName;
public void setLastName(String lastName) {
this.lastName = lastName;
public String toString() {
StringBuffer contactDTOStr = new StringBuffer("Person=[");
contactDTOStr.append(" Mail = " + mail);
contactDTOStr.append(", Common Name = " + commonName);
contactDTOStr.append(", Last Name = " + lastName);
contactDTOStr.append(", Description = " + description);
contactDTOStr.append(" ]");
return contactDTOStr.toString();
public String getMail() {
System.out.println("Mail: " + mail);
return mail;
public void setMail(String mail) {
this.mail = mail;

8. Create ContactAttributeMapper

package com.ldap.examples;
import javax.naming.NamingException;
import org.springframework.ldap.core.AttributesMapper;

public class ContactAttributeMapper implements AttributesMapper{
public Object mapFromAttributes(Attributes attributes) throws NamingException {
ContactDTO contactDTO = new ContactDTO();
String commonName = (String)attributes.get("cn").get();
if(commonName != null)
String lastName = (String)attributes.get("sn").get();
if(lastName != null)
Attribute description = attributes.get("description");
if(description != null)
Attribute mail = attributes.get("mail");
if(mail != null){
contactDTO.setMail((String) mail.get());
return contactDTO;

That's all. Thanks to the Spring LDAP team!!!

No comments: