Spring LDAP Template

Just like you have JDBC/Hibernate/iBatis templates in Spring, we also have an LDAPTemplate. You can download the spring LDAP library fromĀ http://springframework.org/ldap. The template pattern used here lets us avoid common pitfalls such as not cleaning up resources after using an API (in JDBC its the connection, statement and resultset). Why bother when the template can do this for you! Same holds true for LDAP queries.

For this example I had the following setup:

  • Apache Directory Server 1.5.2. I decided to use the sample
    directory data.
  • Installed the Apache Directory Studio eclipse plugin.

To confirm your setup. Open eclipse and go to the LDAP perspective.
Create a new connection with following information:

  • hostname – localhost
  • port – 10389
  • Bind DN or user – uid=admin,ou=system
  • password – secret (this is the default password for apache

This should let you into the directory. Under dc=example,dc=com I added two organizations (asia and americas).

Now for the Spring stuff.

The spring XML file looks like:

Everything above is self explanatory. Now for the test case to execute all of this.

Running this unit test results in output