Skip to content

Instantly share code, notes, and snippets.

@z720
Created April 17, 2012 20:54
Show Gist options
  • Save z720/2408991 to your computer and use it in GitHub Desktop.
Save z720/2408991 to your computer and use it in GitHub Desktop.
Ldap authentication
// Set up the environment for creating the initial context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");
// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");
// Create the initial context
DirContext ctx = new InitialDirContext(env);
// ... do something useful with ctx
javax.naming.AuthenticationException: [LDAP: Invalid Credentials]
at java.lang.Throwable.<init>(Compiled Code)
at java.lang.Exception.<init>(Compiled Code)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment