Java to LDAP Tutorial (Including How to Install an LDAP Server / Client)
This tutorial will show you how to write Java code to interact with a LDAP. But before we can do that we will need to setup an LDAP server and client on our machine.
If at this point you are not sure of exactly what LDAP is, I recommend this post which provides an excellent definition with examples. (In a nutshell it helps to think of an LDAP server as a specialised database).
Installing an LDAP Server
I’m running on a MBP. After looking around for a while I found that the easiest LDAP Server to install was ApacheDirectory which you can download from here. (To install and start the server should take less than 5 minutes)
Once it’s installed it automatically starts the daemon. You can then run the server with this command.
sudo launchctl start org.apache.directory.server
For further installation instructions see here.
LDAP Client
You will want to view the contents of your LDAP Server. The easiest LDAP client to install is Apache Directory Studio which can be downloaded from here.
Once it is downloaded you need to create a connection to the server – the instructions for which are contained here.
When connected your Apache Directory Studio should look something like this:
Now to access LDAP from a Java program. The best way to show you how to do this is through an example program. The program will perform the following tasks:
- Create an new LDAP object
- View an LDAP object
- Add a new attribute to an LDAP object
- Modify an attribute on an LDAP object
- Remove an attribute on an LDAP object
- Delete an LDAP object
Note: This class cleans up after itself i.e. It leaves the LDAP Server in the state in which it was found. If you want to see the various tasks in action just run one of the tasks and take a look at the LDAP Object through the LDAP Client. Don’t forget you can modify the object in the LDAP Client and test in that way.
package test; import java.util.Properties; import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.*; public class LDAPTest { public void run() { try { DirContext context = getContext(); String name = "employeeNumber=00001,ou=system"; createLDAPObject(context, name); createAttribute(context, name, "displayName", "JOBS"); viewAttribute(context, name, "displayName"); updateAttribute(context, name, "displayName", "STEVE"); viewAttribute(context, name, "displayName"); removeAttribute(context, name, "displayName"); removeLDAPObject(context, name); } catch (NamingException e) { e.printStackTrace(); } } private void removeLDAPObject(DirContext context, String name) throws NamingException { context.destroySubcontext(name); } private void createLDAPObject(DirContext context, String name) throws NamingException { Attributes attributes = new BasicAttributes(); Attribute attribute = new BasicAttribute("objectClass"); attribute.add("inetOrgPerson"); attributes.put(attribute); Attribute sn = new BasicAttribute("sn"); sn.add("Steve"); attributes.put(sn); Attribute cn = new BasicAttribute("cn"); cn.add("Jobs"); attributes.put(cn); attributes.put("telephoneNumber", "123456"); context.createSubcontext(name, attributes); } private void removeAttribute(DirContext context, String name , String attrName) throws NamingException { Attribute attribute = new BasicAttribute(attrName); ModificationItem[] item = new ModificationItem[1]; item[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attribute); context.modifyAttributes(name, item); } private void createAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException { Attribute attribute = new BasicAttribute(attrName, attrValue); ModificationItem[] item = new ModificationItem[1]; item[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attribute); context.modifyAttributes(name, item); } private void updateAttribute(DirContext context, String name , String attrName, Object attrValue) throws NamingException { Attribute attribute = new BasicAttribute(attrName, attrValue); ModificationItem[] item = new ModificationItem[1]; item[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute); context.modifyAttributes(name, item); } private void viewAttribute(DirContext context, String name , String attrName) throws NamingException { Attributes attrs = context.getAttributes(name); System.out.println(attrName + ":" + attrs.get(attrName).get()); } private DirContext getContext() throws NamingException { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); properties.put(Context.PROVIDER_URL, "ldap://localhost:10389"); return new InitialDirContext(properties); } public static void main(String[] args) { new LDAPTest().run(); } }
The code is below and should be self explanatory.
Reference: | Java to LDAP Tutorial (Including How to Install an LDAP Server / Client) from our JCG partner Daniel Shaya at the Rational Java blog. |