Quantcast
Viewing all articles
Browse latest Browse all 9

Spring LDAP configuration tutorial

Spring LDAP is an open source Java library designed to simplify LDAP programming on the Java platform. Just as the Spring Framework takes much of the low-level programming out of Java enterprise application development, Spring LDAP frees you from the infrastructural details of using LDAP. Rather than worrying about NamingExceptions and getting InitialContexts, you are free to concentrate on your application’s business logic. Spring LDAP also defines a comprehensive unchecked exception hierarchy and provides helper classes for building LDAP filters and distinguished names.

Spring LDAP is a Spring-based framework that simplifies LDAP programming on the Java platform. In this step-by-step guide to using Spring LDAP you will learn how the framework handles the low-level coding required by most LDAP clients, so that you can focus on developing your application’s business logic. You will also practice simple CRUD operations using Spring LDAP and learn about more advanced operations such as creating dynamic filters and converting LDAP entries into Java beans.

I am discussing spring-ldap configuration and authentication in this tutorial. Along-with other spring jars you need spring-ldap.jar and spring-security-core.jar for this project.

Create a JAVA project.

create context.properties file in src folder.

Add following content in context.properties file.

 ldap.url=ldap://***********
 ldap.base=**********
 ldap.userdn=***********
 ldap.password=***********

Replace these with Your values.

In the same hierarchy, create ldap-config.xml file

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="propertyPlaceHolderPMSServices"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="locations">
 <list>
 <value>classpath:context.properties</value>
 </list>
 </property>
 </bean>

<bean id="contextSource"
 class="org.springframework.ldap.core.support.LdapContextSource">
 <property name="url" value="${ldap.url}" />
 <property name="userDn" value="${ldap.userdn}" />
 <property name="password" value="${ldap.password}" />
 </bean>

<bean id="ldapTemplate"
 class="org.springframework.ldap.core.LdapTemplate">
 <constructor-arg ref="contextSource" />
 </bean>
 <bean id="personDao">
 <property name="ldapTemplate" ref="ldapTemplate" />
 </bean>

</beans>

Create PersonDaoImpl.java and PersonDao.java (interface) in com.example.dao package.

Basic code that will go into PersonDaoImpl.java class is:

public class PersonDaoImpl implements PersonDao {

private LdapTemplate ldapTemplate;

public void setLdapTemplate(LdapTemplate ldapTemplate) {
 this.ldapTemplate = ldapTemplate;
 }

// OTHER METHODS FOR SEARCHING AND AUTHENTICATION

}

 

Create a TestLDAP.java class having main method in it.

public static void main(String[] args) {

ApplicationContext factory = new ClassPathXmlApplicationContext("ldap-config.xml");

PersonDao personDao = (PersonDao) factory.getBean("personDao");

//call any method here like   personDao.searchInLdap(base, filter);

}

Image may be NSFW.
Clik here to view.
Share


Viewing all articles
Browse latest Browse all 9

Trending Articles