1

I have written a small program to authenticate against ldap.

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

class SAuth {
    public static void main(String[] args) {

    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
        "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://xx.xx.xx.xx:yyyy/");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "username");
    env.put(Context.SECURITY_PRINCIPAL, "cn=orcladmin");
    env.put(Context.SECURITY_CREDENTIALS, "password");

    try {

        DirContext ctx = new InitialDirContext(env);
            System.out.println(" i guess the connection is sucessfull :)");

        // Do something useful with ctx 

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }

} }

I get the following error :

javax.naming.AuthenticationNotSupportedException: orcladmin
        at com.sun.jndi.ldap.sasl.LdapSasl.saslBind(LdapSasl.java:100)
        at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:214)
        at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2658)
        at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:287)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)

        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193
)
        at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.ja
va:136)
        at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.jav
a:66)
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
67)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288
)
        at javax.naming.InitialContext.init(InitialContext.java:223)
        at javax.naming.InitialContext.<init>(InitialContext.java:197)
        at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.jav
a:82)
        at Simple.main(Simple.java:28)

But , if i try to access ldap directory using ldap cmd line and same user credentials , it works smoothly. for example:

ldapsearch -p <port> -h <ip> -D "cn=orcladmin" -w "password"  objectClass=*

returns concerned data. I guess there is something wrong with the java program , but dunno wat .

3

1 Answer 1

3

Try chaning

env.put(Context.SECURITY_AUTHENTICATION, "username");

to

env.put(Context.SECURITY_AUTHENTICATION, "simple");
Sign up to request clarification or add additional context in comments.

2 Comments

it worked .. finally. is there any books which explain various ldap concepts and APIs using Java ?
There is an immense JNDI Tutorial at oracle.com including a major section on LDAP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.