tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > LDAP > Login to LDAP Directory as Specified User

Login to LDAP Directory as Specified User 

LDAP (Lightweight Directory Access Protocol) is based on X.500 standard. Its a hierarchical data structure with Entries organized in a tree like structure called Directory Information Tree (DIT). The following example shows how to login to LDAP Directory as specified user.

directory.png

File Name  :  
com/bethecoder/tutorials/ldap/LoginUserTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.ldap;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class LoginUserTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //Setup the environment to login as 'uid=sram'
    String userDN = "uid=sram,ou=home_team,ou=People,dc=test,dc=com";
    String userPWD = "abcd1234";
    
    Hashtable<String, String> environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, "ldap://localhost:389");
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, userDN);
    environment.put(Context.SECURITY_CREDENTIALS, userPWD);

    DirContext dirContext = null;
    
    try {
      dirContext = new InitialDirContext(environment);
      System.out.println("Connection established as user");

    catch (NamingException e) {
      e.printStackTrace();
    finally {
      if (dirContext != null) {
        try {
          dirContext.close();
        catch (Exception e) {
        }
      }
    }
  }

}
   

It gives the following output,
Connection established as user



 
  


  
bl  br