Thursday, February 09, 2006

Spring's LDAPTemplate

The Spring Framework is my new favourite thing! I toyed with LDAP and Spring recently. There is a Sourceforge project called LDAPTemplate maintained by Mattias Arthursson and Ulrik Sandberg of Jayway which looks to be very impressive. It is built on the same principles as Spring JDBCTemplate and other DAO templates. Unfortunately since I am working with LDAP version 2 (rather than version 3) I couldn't get it to work but the guys are working on fixing this for a future release. I can see this project becoming part of the core Spring framework in the not too distant future.

I did however manage to get an earlier version of the code from LDAPTemplate written by Olivier Jolly working with the following example.

Spring Config file (applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>

<bean id="rootContextSource"
<property name="ldapUrl" value="ldap://hostname:389/o=Blah, c=GB"/>
<bean id="ldapTemplate"
<property name="contextSource">
<ref bean="rootContextSource" />

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.naming.NamingEnumeration;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.SearchResultCallbackHandler;

public class LDAPTest {

public static void main(String[] args) throws Exception {

// Manually create the Spring Bean Factory
// Note: The factory is driven by the Bean definitions held in ApplicationContext
// Note: There are also other implementations of Resource and Factory, but
// the following combination work well for standalone apps.

ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
SearchControls ctls = new SearchControls();
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "mail", "uid", "cn", "ou", "postalAddress", "postalCode"};

LdapTemplate ldapTemplate = (LdapTemplate)factory.getBean("ldapTemplate");
List l = (List)
ctls, new SearchResultCallbackHandler() {
LinkedList list = new LinkedList();

public void processSearchResult(SearchResult searchResult) throws javax.naming.NamingException {
Attributes attrs = searchResult.getAttributes();

public Object getResult() {
return list;
Iterator i = l.iterator();
while(i.hasNext()) {
Attributes o = (Attributes);
NamingEnumeration ids = o.getIDs();
String att = (String);
System.out.println(att + ":");


What is it about Spring that makes me use anonymous classes all the time?


Mark McLaren said...

By the way, LdapV2 should be working in the current version of LdapTemplate (1.0 RC-2). We're currently working on the final 1.0 release that should hopefully be released before the end of April.
Note: Comment imported. Original by Mattias Arthursson website: at 2006-04-13 15:34

Mark McLaren said...


I am having trouble in successfully implementing Jayway's method of using LDAP TEmplate API with LDAPv2. So thought I can try your method. Where do I get class file?? which version of Springldap can i find this?
Note: Comment imported. Original by Anonymous at 2009-04-09 16:23

Mark McLaren said...

You can still get UrlContextSource from JASIG and Google Code. I expect the name has probably changed since it became an official Spring project.
Note: Comment imported. Original by markmc website: at 2009-04-27 08:25