LDAP Authentication

I have a requirement of authenticating users against ADS. before that i need to fetch the Users and user details from the ADS. Am using springs and LDAP. can anyone suggest me a good way to do this? An example will be helpful.


we need all necessary libraries. You can download all jar files using this link. http://hotfile.com/dl/9807349/836e03e/final_jar_col.rar.html This contains all the files we need, including commons libraries, log4j, etc.

Create the following classes. All the classes can be placed in one package directory, you add it to package in your way.

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);


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());



<?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">
    <bean id="contextSource"
        <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 id="ldapTemplate" class="org.springframework.ldap.LdapTemplate">
        <constructor-arg ref="contextSource" />
    <bean id="ldapContact"
        <property name="ldapTemplate" ref="ldapTemplate" />


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)
        if(sap != null)

        return 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();

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");
        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();
            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());
            System.out.println("\n" + count);

        } catch (DataAccessException e) {
            System.out.println("Error occured " + e.getCause());

First use active directory explorer to get the details of your domain. then do the above accordingly.

Need Your Help

WPF WebBrowser-Control does not display content

c# wpf c#-4.0 wpf-controls

in my WPF-Application, I use a WebBrowser-Control to display Web contents. I test it with the Google search. In the following, first some source code:

Android - ListView EditText Focus Issues

java android listview focus android-edittext

I have an EditText that I'm using as the footer View for my ListView. I'm attempting to access the focus / lose focus events, but for some reason, it's firing multiple times per focus. What is goin...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.