user1177755
user1177755

Reputation: 85

How to bind in Java using DN and Password in LDAP?

I want to search a user from LDAP and after getting the user I want to connect (validate) that particular user using his DN and Password I have successfully getting the DN but dont know how to bind it?

Upvotes: 2

Views: 23117

Answers (4)

Tharok
Tharok

Reputation: 1041

If you already have LdapContext opened using your credentials, you can copy it, change principal+credential in its environment and try reconnect:

LdapContext userContext = ldapContext.newInstance(null); // copy context
userContext.addToEnvironment(InitialDirContext.SECURITY_PRINCIPAL, userDn);
userContext.addToEnvironment(InitialDirContext.SECURITY_CREDENTIALS, password);
userContext.reconnect(null); // throws NamingException if creds wrong
userContext.close();

If it throws NamingException, credentials are wrong. It it is successful, credentials are ok. ;)

(This is useful if you have only LdapContext, but not the InitialDirContext, available.)

Upvotes: 0

user207421
user207421

Reputation: 310916

The LDAP bind() operation corresponds to the following in JNDI:

  1. Constructing an InitialDirContext or InitialLdapContext with enough information in the environment to cause a login, i.e. a security principal and credentials, or

  2. Calling reconnect() on an LdapContext initially obtained without any security information in the environment, or with security information relating to a different principal, but whose environment has subsequently been modified.

Upvotes: 2

Terry Gardner
Terry Gardner

Reputation: 11134

When a connection is made to a directory server using LDAP, the connection state is unauthenticated. Requests can be transmitted on an unauthenticated connection, assuming the server administrators permit unauthenticated requests. The BIND request is used to change authentication state of a connection.

Here is an example of searching and authenticating using the UnboundID LDAP SDK: SimpleBindExample.java. This example searches for an entry given a base object, naming attribute, and username, and then attempts to authenticate using a simple bind. Examples using a SASL bind could be constructed just as easily.

Upvotes: 1

Houcem Berrayana
Houcem Berrayana

Reputation: 3080

Here is an example that I took from the official documentation :

// Set up the environment for creating the initial context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

// Authenticate as S. User and password "mysecret"
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
env.put(Context.SECURITY_CREDENTIALS, "mysecret");
DirContext ctx = new InitialDirContext(env);

You have to choose your right authentication model. I have tried it before and worked fine.

Upvotes: 4

Related Questions