Created
July 18, 2018 18:39
-
-
Save kevinmmartins/597b79df1eff7cc329652fe724c34c37 to your computer and use it in GitHub Desktop.
Class to Authenticate a user in LDAP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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