简体   繁体   中英

java program unable to authenticate against ldap

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 .

Try chaning

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM