Authenticating to the Directory - Java JNDI

Java examples for JNDI:Context

Description

Authenticating to the Directory

Demo Code

import java.util.Hashtable;

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

public class Main {
  public static void m() {
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "userDN");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    try {// w  w w  . j  a  v  a  2  s . c om
      DirContext ctx = new InitialDirContext(env);
    } catch (NamingException e) {
    }
  }
}

Related Tutorials