September 12, 2006

LDAP / JNDI Context



/*
This is a simple example on how to use JNDI to create DirContext into LDAP server.

The simple authentication is a plantext password.
*/

private InitialLdapContext ctx(String principal,String credentials) throws NamingException {

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://hostname:post");

if ( info.principal != null && info.credentials != null ) {
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, principal );
env.put(Context.SECURITY_CREDENTIALS, credentials );
} else {
env.put(Context.SECURITY_AUTHENTICATION, "none");
}

return new InitialLdapContext(env,new Control[0]);
}

No comments: