Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ldap authentication issue

I'm trying to have my custom java application use our Active Directory Server for authentication but I cannot get it to work for some reason. Can anyone see why this is? Here is my method below:

private boolean authenticate(String serverName, String userId, String password) throws NamingException {
    DirContext ctx = null;
    Hashtable env = new Hashtable(11);
    boolean b = false;
    try {
        env.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://servername.org:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid="+ userId +",ou=All Users,dc=site,dc=org");
        env.put(Context.SECURITY_CREDENTIALS, password);
        System.out.println("before context");
        // If there isn't a naming exception then the user is authenticated. Return true
        ctx = new InitialDirContext(env);
        //The user is authenticated.
        b = true;
    } catch (NamingException e) {
        System.out.println("the user is not authenticated return false");
        b = false;
    }finally{
        if(ctx != null)
            ctx.close();
    }
    return b;
}

Result:

[12/14/11 16:27:47:746 CST] 0000001f SystemErr     R
javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece
like image 558
bschupbach Avatar asked Dec 14 '11 21:12

bschupbach


People also ask

How does LDAP authentication work in Java?

In short, a client sends a request for information stored within an LDAP database along with the user's credentials to an LDAP server. The LDAP server then authenticates the credentials submitted by the user against their core user identity, which is stored in the LDAP database.

Does LDAP use Java?

It accepts as parameters the URL of the LDAP server, the principal user and its password, the branch where the users are stored, and the user name. It uses the standard Java package javax. naming.

What is LDAP authentication failed?

Authentication to the LDAP server is done through a binding in the form of either a distinguished name or anonymous login. Having an incorrect bind is the most common reason for seeing the Authentication Failed error when attempting to import Users/Groups or test Users/Groups on the SonicWall.


1 Answers

Have you tried this way?

//...
env.put(Context.SECURITY_PRINCIPAL, "cn="+ userId +",ou=All Users,dc=site,dc=org");
//...

Also replace

Hashtable env = new Hashtable(11);

with

Hashtable env = new Hashtable();
like image 117
JuanZe Avatar answered Sep 28 '22 22:09

JuanZe