Ravo
Ravo

Reputation: 41

connecting to open LDAP using java

I have installed open LDAP which is ruinning on port default port 389, i want to search all people in the directory, i am using code i found on this site

          final String ldapAdServer = "ldap://";
    final String ldapSearchBase = "ou=People,dc=maxcrc,dc=com";

    final String ldapUsername = "Manager";
    final String ldapPassword = "secret";

    final String ldapAccountToLookup = "*";


    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    if(ldapUsername != null) {
        env.put(Context.SECURITY_PRINCIPAL, ldapUsername);
    }
    if(ldapPassword != null) {
        env.put(Context.SECURITY_CREDENTIALS, ldapPassword);
    }
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapAdServer);

    //ensures that objectSID attribute values
    //will be returned as a byte[] instead of a String
    env.put("java.naming.ldap.attributes.binary", "objectSID");

    // the following is helpful in debugging errors
    //env.put("com.sun.jndi.ldap.trace.ber", System.err);
    Context ctx = new InitialContext(env);
    LdapContext ctxLap = new InitialLdapContext();

but getting error like

Exception in thread "main" javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]

i think this is problem with host name ,which i am not able find correctly how can i find url to which i need to connect?

Upvotes: 0

Views: 3021

Answers (1)

user207421
user207421

Reputation: 311054

Exception in thread "main" javax.naming.InvalidNameException: [LDAP: error code 34 - invalid DN]

I think this is problem with host name, which i am not able find correctly

No. It's a problem with a DN. A DN is not a hostname. If it was a hostname problem, you would get a connect exception of some kind. If you get an LDAP error code, it is proof you are connected to an LDAP server.

It's probably a problem with the Manager DN. But you shouldn't be logging in as the LDAP root account anyway. That's for OpenLDAP itself. Create another account with sufficient privilege and use that.

Upvotes: 1

Related Questions