Pages

Thursday, July 7, 2016

Connect Ldap using Java

LDAP (Lightweight Directory Access Protocol) is a software protocol for enabling anyone to locate organizations, individuals, and other resources such as files and devices in a network, whether on the public Internet or on a corporate intranet. LDAP is a "lightweight" (smaller amount of code) version of Directory Access Protocol (DAP), which is part of X.500, a standard for directory services in a network.
Directory structure strings:
rdnRelative Distinguished Name
dnDistinguished Name
cnCommon Name
ouOrganizational Unit
dcDomain Component
snSurName
As an example, the Entry look like was:
dn: cn=Joe Smith,ou=East,dc=MyDomain,dc=com
 cn: John Doe
 givenName: John
 sn: Doe
 telephoneNumber: +1 888 555 6789
 telephoneNumber: +1 888 555 1232
 mail: john@example.com
 manager: cn=Barbara Doe,dc=example,dc=com
 objectClass: inetOrgPerson
 objectClass: organizationalPerson
 objectClass: person
 objectClass: top

Throwable Object Connect ldap using java ldap binding structure
Above example, the Distinguished Name "cn=Joe Smith,ou=East,dc=MyDomain,dc=com" has four components. The first (lowest level) component of the Distinguished Name is the Relative Distinguished Name (RDN) of the object. In this case, the RDN is "cn=Joe Smith". The RDN of an object is the name of the object in its container. The remainder of the components are the Distinguished Name of the container, which is the parent of the object. In this case, the object "cn=Joe Smith" is in the container whose Distinguished Name is "ou=East,dc=MyDomain,dc=com". In this case, the parent container is an organizational unit. The parent of the "ou=East" organizational unit is the domain "MyDomain.com". This domain has domain components "dc=MyDomain" and "dc=com". The full DNS name of the domain is "dc=MyDomain,dc=com".
Container objects can be containers, organizational units, or domains. Container objects are objects that can "contain" other objects, such as user objects, group objects, and computer objects. Group objects are not containers. Groups can have members, but the members are not children of the group object.


Sample java program to connect ldap and search the directory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;
import javax.naming.NamingEnumeration;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attribute;

public class LdapTest 
{
  public static void main(String[] args)
  {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:392"));
    env.put("com.sun.jndi.ldap.connect.pool", "true");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "password");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put("com.sun.jndi.ldap.read.timeout", "50000");
    env.put("com.sun.jndi.ldap.connect.timeout", "50000");

    try 
    {
      DirContext ctx =  new InitialDirContext(env);
      Attributes matchAttrs = new BasicAttributes(true);
      matchAttrs.put(new BasicAttribute("inputAttributeName", "inputAttributeValue"));      
      NamingEnumeration<SearchResult> answer = ctx.search("baseDN",matchAttrs);
      while (answer.hasMore()) 
      {
        SearchResult sr = answer.next();
        NamingEnumeration<? extends Attribute> attributes = sr.getAttributes().getAll();
        while (attributes.hasMore()) 
        {
          Attribute anAttribute = attributes.next();
          String id = anAttribute.getID();
          String value = anAttribute.get().toString();
          System.out.println(id+" "+value);
        }
        attributes.close();
      }
      answer.close();
    } catch (NamingException e) {
        e.printStackTrace();
    } 
    finally 
    {
        if (ctx != null) 
        {
          try 
          {
              ctx.close();
          } catch (NamingException e) {
              e.printStackTrace();
          }
        }
    }
  }
}

No comments:

Post a Comment