Skip to content

Instantly share code, notes, and snippets.

@kevinmmartins
Created July 18, 2018 18:39
Show Gist options
  • Save kevinmmartins/597b79df1eff7cc329652fe724c34c37 to your computer and use it in GitHub Desktop.
Save kevinmmartins/597b79df1eff7cc329652fe724c34c37 to your computer and use it in GitHub Desktop.
Class to Authenticate a user in LDAP
package com.test.ldap;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LdapConnection {
public static void main(String args[]) {
String serverURL = "ldap://localhost:389";
String bindDN = "cn=Kauan Martins,cn=Kevin Martins,cn=developers,ou=users,dc=ictsldap,dc=com";
String bindPassword = "notMyPass";
Properties parms = new Properties();
parms.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
parms.put(Context.PROVIDER_URL, serverURL);
parms.put(Context.SECURITY_AUTHENTICATION, "simple");
parms.put(Context.SECURITY_PRINCIPAL, bindDN);
parms.put(Context.SECURITY_CREDENTIALS, bindPassword);
DirContext ctx = null;
try {
ctx = new InitialDirContext(parms);
System.err.println("Successful authenticated bind");
} catch (NamingException ne) {
System.err.println("Unsuccessful authenticated bind\n");
ne.printStackTrace(System.err);
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException ne) {
System.err.println("Error close resource");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment