AnsweredAssumed Answered

Ldap Connection Problem

Question asked by irvank on Jul 4, 2015
Latest reply on Jul 5, 2015 by mrogers
Hi guys i have a problem with my ldap connection via java, i'm using debian here

this is my reference http://techpubs.spinlocksolutions.com/dklar/ldap.html
i have following this as reference for installing ldap and configuring it, and it work perfectly on terminal, but i cant connect it to java.


package com.irvankosini;

import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.AuthenticationNotSupportedException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class ldap {
    public static void main(String[] args) {
       
   
    String url = "ldap://192.168.1.1:389";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid=mirko,ou=people");
env.put(Context.SECURITY_CREDENTIALS, "kosini");

try {
    DirContext ctx = new InitialDirContext(env);
    System.out.println("connected");
    System.out.println(ctx.getEnvironment());
    
    // do something useful with the context…

    ctx.close();

} catch (AuthenticationNotSupportedException ex) {
    System.out.println("The authentication is not supported by the server");
} catch (AuthenticationException ex) {
     ex.printStackTrace();
} catch (NamingException ex) {
    System.out.println("error when trying to create the context");
}
}}


this is the error log
run:
javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]
   at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3135)   at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3135)
   at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3081)
   at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2883)
   at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2797)
   at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:319)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:192)
   at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:210)
   at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:153)
   at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:83)
   at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
   at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
   at javax.naming.InitialContext.init(InitialContext.java:244)
   at javax.naming.InitialContext.<init>(InitialContext.java:216)
   at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:101)
   at com.irvankosini.ldap.main(ldap.java:25)
   at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3135)
BUILD SUCCESSFUL (total time: 3 seconds)

Outcomes