asked on
public static void main(String[] args)
{
String username = "jsmith";
String password = "password@123";
String base = "ou=people,dc=nodomain";
String dn = "uid=" + username + "," + base;
String ldapURL = "ldap://xx.xx.xx.xx:389";
// Setup environment for authenticating
Hashtable<String, String> environment =
new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, dn);
environment.put(Context.SECURITY_CREDENTIALS, password);
try
{
DirContext authContext =
new InitialDirContext(environment);
System.out.println("authentication success!");
// user is authenticated
}
catch (AuthenticationException ex)
{
System.out.println("authentication failed!"); // I'm getting this print
// Authentication failed
}
catch (NamingException ex)
{
ex.printStackTrace();
}
}
}
ASKER
dn: cn=S K Das,ou=people,dc=nodomain
objectclass: inetOrgPerson
cn: S K Das
sn: skda
uid: skdas
userPassword: daSsk
carlicense: ABCD 123
homephone: 123-111-2456
mail: skdas1@nodomain.com
mail: skdas2@nodomain.com
mail: skdas3@nodomain.com
description: swell guy
ou: Human Resources
public static void main(String[] args)
{
String username = "skdas";
String password = "daSsk";
String base = "cn=S K Das,ou=people,dc=nodomain";
String dn = "uid=" + username + "," + base;
String ldapURL = "ldap://xx.xx.xx.xxx:389";
// Setup environment for authenticating
Hashtable<String, String> environment =
new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, dn);
environment.put(Context.SECURITY_CREDENTIALS, password);
try
{
DirContext authContext =
new InitialDirContext(environment);
System.out.println("authentication success!");
// user is authenticated
}
catch (AuthenticationException ex)
{
ex.printStackTrace();
System.out.println("authentication failed!");
// Authentication failed
}
catch (NamingException ex)
{
ex.printStackTrace();
}
}
StackTrace :
javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at javax.naming.directory.InitialDirContext.<init>(Unknown Source)
at com.techm.bm.LdapConnect.main(LdapConnect.java:96)
authentication failed!
ASKER
try
environment.put(Context.SECURITY_PRINCIPAL, base);
ASKER
The username is cn is it not?
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
ASKER