Results 1 to 4 of 4

Thread: Introduction to Spring LDAP

  1. #1
    Join Date
    Feb 2010
    Posts
    136

    Introduction to Spring LDAP

    I. Introduction
    It is not necessary to use or know Spring to use this tutorial. It is not necessary to install an LDAP server, because we will use the server Public www.openldap.com. The source code which are used in this tutorial are at the bottom of the tutorial.

    II. Installing an LDAP browser
    To view the contents of the LDAP server, we need an LDAP browser. I suggest you use Softer LDAP Browser, you may even you any other browser of your choice.

    - Download and install Softer LDAP Browser from their official site and do a full install.
    - Use the browser to go to the prerecorded OpenLDAP server (www.openldap.com on port 389)
    - Open successively Nodes: OpenLDAP / cn = DirectoryManager = People. Select the node with the uid = Kurt.

    Right click in the properties of Kurt, you can see the full address of the Kurt Server:
    ldap: / / www.openldap.com:389/uid=kurt, ou = People, dc = OpenLDAP, dc = Org

    This address consists of 2 parts:
    Part 1: www.openldap.com:389 is the internet address and port of the LDAP server.

    Part 2: uid = kurt, ou = People, dc = OpenLDAP, dc = Org is the Distinguish Name of kurt registration. It represents the path in the LDAP tree kurt.

    III. Download Spring LDAP
    Download Library spring-ldap with dependencies ( a zip file) from the official site. This should be easy.

    Unzip this archive.
    In the directory dist is spring-ldap.jar which must be placed in the CLASSPATH project. In repertory lib is lang.jar-commons, commons-loggin.jar, springbeans. jar, spring-core.jar. All these libraries must be added to CLASSPATH.

  2. #2
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to Spring LDAP

    IV. Creating a connection to the LDAP server.
    Spring LDAP uses the concept of ContextSource to define a connection to the LDAP server. Create a factory to retrieve a ContextSource correct. ContextSource being a interface, we use LdapConSrc which is an implementation provided by Spring LDAP.
    Here is the code:

    Code:
    Public class LdapContextSourceFactory {
    	Public static ContextSource getLdapContextSource() throws Exception {
    		ldapconsrc consrc = new ldapconsrc();
    consrc.setUrl("ldap: / / www.openldap.com:389");
    consrc.setBase("dc = OpenLDAP, dc = org");
    consrc.afterPropertiesSet();
    		return consrc;
    	}
    }
    V. Creating the Subject Person
    Spring LDAP can play recordings of people as Person object. Here is our Person class we will get an instance of Kurt. Uid defines the unique identifier on the LDAP server for Person
    Code:
    Public class Person {
    	private String id;
    	private String frnm;
    	private String lsnm;
    	Public String getFirstName() {
    		return frnm;
    	}	
    	Public void setFirstName(String frnm) {
    		this. frnm = frnm;
    	}
    	Public String getLastName() {
    		return lsnm;
    	}
    	Public void setLastName(String lsnm) {
    		this. lsnm = lsnm;
    	}
    	Public String getuid() {
    		return id;
    	}
    	Public void setuid(String id) {
    		this. id = id;
    	}
    }
    VI. Recovery of a Person object via a DAO.
    It is possible to use the DAO pattern to access the Person objects. Here's how proceed in 4 steps

    - 1 Creating a Mapper LDAP to Java
    A AttributeMapper can correctly instantiate our Person class. It was he who performs the mapping between the structure and the LDAP Person Object Model Java Person. This map is declared as internal internal CAD, because it should not be used outside in this DAO.
    Code:
    Public class PersonDao {
    	private static class prsattmap implements AttributesMapper
    	{
    		Public Person mapFromAttributes(Attributes at)
    				throws javax.naming.NamingException {
    			Person p = new Person();
    p.setFirstName(at.get("givenName").get().function toString() {
        [native code]
    }());
    p.setLastName(at.get("sn").get().function toString() {
        [native code]
    }());
    p.setuid(at.get("uid").get().function toString() {
        [native code]
    }());
    p.setEmail(at.get("mail").get().function toString() {
        [native code]
    }());
    			return p;
    		}
    	}
    }
    - 2 Use LdapTemplate
    The implementation of LDAP queries is done via a LdapTemplate.
    Here is the code for it
    Code:
    Public class PersonDao {
    	...
    	private LdapTemplate ldaptem;
    	Public void setLdapTemplate(LdapTemplate ldaptem) {
    		this. ldaptem = ldaptem;
    	}
    	...
    }
    - 3 Set Distinguish Name
    The Distinguish Name is the path to registration of Person on the server. The BuildDn function gives a proper Distinguish Name.
    Here is the code for it
    Code:
    Public class PersonDao {
    	...
    	private Name buildDn(String id) {
    		DistinguishedName dis = new DistinguishedName();
    dis.add("or", "People");
    dis.add("id", Uid);
    		return dis;
    	}
    	...
    }
    - 4 get the instance of the class Person
    Simply use the lookup method of ldapTemplate for instance corresponding to primary key, i.e the id in the case of Subject Person.
    Here is the code for it
    Code:
    Public class PersonDao {
    	...
    	Public Person findByPrimaryKey(String id) {
    		Name dis = buildDn(id);
    		return (Person) ldapTemplate.lookup(dis new
    		prsattmap());
    	}
    	...
    }

  3. #3
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to Spring LDAP

    VII. Test for DAO

    - Let's create a class executable to test our DAO.

    Code:
    Public class TestLdap {
    	/** Retrieve has Kurt Person from LDAP server and display Kurt in
    	Standard Out */
    	Public static void hand(String [] args) {
    	}
    }
    The executable class takes into account any argument.


    - Let's add the main method in the Context Source
    Code:
    // 1 Retrieve has LdapContextSource
    ContextSource consrc = null;
    try {
    	consrc = LdapContextSourceFactory.getLdapContextSource();
    } catch (Exception e) {
    	System.out.System.out.println("Unable to get has LdapContextSource.");
    e.printStackTrace();
    }
    - It must also obtain a LdapTemplate.
    Code:
    // 2 Instanciate has LdapTemplate
    LdapTemplate ldaptem = new LdapTemplate();
    ldaptem.setContextSource(ldapContextSource);
    - Instantiating a Dao and provide him ldapTemplate.

    Code:
    // 3 instanciate has PersonDao
    PersonDao c = new PersonDao();
    c.setLdapTemplate(ldapTemplate);
    - Recovery of Kurt is just using the DAO.
    Code:
    // 4 retrieve has Person and display it
    Person prs = cad.findByPrimaryKey("kurt");
    System.out.System.out.println("Uid: " + prs.getuid());
    System.out.System.out.println("FirstName: " + prs.getFirstName());
    System.out.System.out.println("LastName: " + prs.getLastName());
    System.out.System.out.println("Email: " + prs.getEmail() + "\ n");
    Execute the program and you will get the desired results.


    VIII. Improved CAD
    Add a method that allows the DAO to retrieve a list of people from their name. The class LikeFilter can use filters with wildcards searches:
    Here is the code for it
    Code:
    Public List getPersonNamesByLastName(String lsnm) {
    	AndFilter flt = new AndFilter();
    flt.and(new EqualsFilter("objectclass", "person"));
    flt.and(new LikeFilter("sn", lsnm));
    	return ldapTemplate.search("", Filter.encodes(),
    	new PersonAttributMapper());
    }
    - Complement the main class with an example of using this new feature.
    Code:
    // 5 retrieve has list of person
    List lsprs = cad.getPersonNamesByLastName("* e *");
    for (Object object: lsprs) {
    	System.out.System.out.println("Person: " + object);
    }
    The execution of the main class returns 2 persons in standard output.


    IX. Conclusion
    Spring LDAP is a library that allows J2EE simply retrieve records LDAP as java objects and free the developer from the Java knowledge LDAP mechanisms. Spring LDAP can also write to the LDAP server. Hope this tutorial was useful.

  4. #4
    Join Date
    Feb 2010
    Posts
    136

    Re: Introduction to Spring LDAP

    X. Source code used in this tutorial

    1- Person.java


    Code:
    Public class Person {
    	private String id;
    	private String frnm;
    	private String lsnm;
    	private String em;
    	Public String getEmail() {
    		return em;
    	}
    	Public void setEmail(String em) {
    		this. em = em;
    	}
    	Public String getFirstName() {
    		return frnm;
    	}
    	Public void setFirstName(String frnm) {
    		this. frnm = frnm;
    	}
    	Public String getLastName() {
    		return lsnm;
    	}
    	Public void setLastName(String lsnm) {
    		this. lsnm = lsnm;
    	}
    	Public String getuid() {
    		return id;
    	}
    	Public void setuid(String id) {
    		this. id = id;
    	}
    	Public String function toString() {
        [native code]
    }(){
    		return id+" - "+frnm+" "+lsnm;
    	}
    }
    2- PersonDao.java
    Code:
    Public class PersonDao {
    	private LdapTemplate ldaptem;
    	private static class prsattmap implements AttributesMapper
    	{
    		Public Person mapFromAttributes(Attributes attrs)
    				throws javax.naming.NamingException {
    			Person prs = new Person();
    prs.setFirstName(attrs.get("givenName").get().function toString() {
        [native code]
    }());
    prs.setLastName(attrs.get("sn").get().function toString() {
        [native code]
    }());
    prs.setuid(attrs.get("id").get().function toString() {
        [native code]
    }());
    prs.setEmail(attrs.get("mail").get().function toString() {
        [native code]
    }());
    			return prs;
    		}
    	}
    	Public Person findByPrimaryKey(String id) {
    		Name d = buildDn(id);
    		return (Person) ldaptem.lookup(d new
    		prsattmap());
    	}
    	private Name buildDn(String id) {
    		DistinguishedName d = new DistinguishedName();
    d.add("or", "People");
    d.add("id", id);
    		return d;
    	}
    	Public void setLdapTemplate(LdapTemplate ldaptem) {
    		this. ldaptem = ldaptem;
    	}
    	Public List getPersonNamesByLastName(String lastName) {
    		AndFilter flt = new AndFilter();
    flt.and(new EqualsFilter("objectclass", "person"));
    flt.and(new LikeFilter("sn", LastName));
    		return ldaptem.search("", Filter.encodes(),
    		new prsattmap());
    	}
    }
    3- LdapContextSourceFactory.java
    Code:
    Public class LdapContextSourceFactory {
    	Public static ContextSource getLdapContextSource() throws Exception {
    		LdapContextSource ldapconsrc = new LdapContextSource();
    ldapconsrc.setUrl("ldap: / / www.openldap.com:389");
    ldapconsrc.setBase("dc = OpenLDAP, dc = org");
    ldapconsrc.afterPropertiesSet();
    		return ldapconsrc;
    	}
    }
    4- TestLdap.java
    Code:
    Public class TestLdap {
    	/** Retrieve has Kurt Person from ldap server and display Kurt in
    	Standard Out */
    	Public static void hand(String [] args) {
    		// 1 Retrieve has LdapContextSource
    		ContextSource ldapconsrc = null;
    		try {
    			ldapconsrc =
    			LdapContextSourceFactory.getLdapContextSource();
    		} catch (Exception e) {
    			System.out.System.out.println("Unable to get has
    			LdapContextSource.");
    			e.printStackTrace();
    		}
    		// 2 Instanciate has LdapTemplate
    		LdapTemplate ldaptem = new LdapTemplate();
    ldaptem.setContextSource(ldapconsrc);
    		// 3 instanciate has PersonDao
    		PersonDao c = new PersonDao();
    c.setLdapTemplate(ldaptem);
    		// 4 retrieve has Person and display it
    		Person prs = c.findByPrimaryKey("kurt");
    System.out.System.out.println("Uid: " + prs.getuid());
    System.out.System.out.println("FirstName: " + prs.getFirstName());
    System.out.System.out.println("LastName: " + prs.getLastName());
    System.out.System.out.println("Email: " + prs.getEmail() + "\ n");
    		// 5 retrieve has list of prs
    		List lstprs = c.getPersonNamesByLastName("* e *");
    		for (Object object: lstprs) {
    			System.out.System.out.println("Person: " + object);
    		}
    	}
    }

Similar Threads

  1. LDAP query to speficied LDAP server on TCP port 389 failed
    By Shash in forum Windows Server Help
    Replies: 2
    Last Post: 02-05-2012, 05:01 PM
  2. Problem in binding the user in LDAP using Spring LDAP
    By deepti.agrawal in forum Software Development
    Replies: 1
    Last Post: 25-04-2011, 03:26 AM
  3. Replies: 1
    Last Post: 24-03-2010, 10:12 PM
  4. What is Spring?
    By Adrina_g in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 01:56 PM
  5. Why Spring, Benefits using spring framework?
    By Mahendra varma in forum Software Development
    Replies: 3
    Last Post: 25-04-2009, 10:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,523,120.87801 seconds with 17 queries