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: http://springframework.org/ldap
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.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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();
     my.setLdapTemplate(ldapContact);
     System.out.println(my.getPersonMail(desiredMail@company.com));
}
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 ldapTemplate.search("", 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" -"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
-??beans>
-?bean id="contextSource"
-?class="org.springframework.ldap.core.support.LdapContextSource">
-?property name="url" value="ldap://ldapServerIPorName:389" />
-?property name="base" value="o=company.com" />
-?/bean>
-?bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
-?constructor-arg ref="contextSource" />
-?/bean>
-?bean id="ldapContact"
-?class="com.ldap.examples.PersonDaoImpl">
-?property name="ldapTemplate" ref="ldapTemplate" />
-?/bean>
-?/beans>
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 javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
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)
     contactDTO.setCommonName(commonName);
String lastName = (String)attributes.get("sn").get();
if(lastName != null)
     contactDTO.setLastName(lastName);
Attribute description = attributes.get("description");
if(description != null)
     contactDTO.setDescription((String)description.get());
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:
Post a Comment