title: LDAP Authentication using Java
body: <p>I need to do LDAP Authentication for an application.</p>
  
  <p>I tried the following program:</p>
  
  <pre><code>import java.util.Hashtable;  
  
  import javax.naming.Context;  
  import javax.naming.NamingException;  
  import javax.naming.ldap.InitialLdapContext;  
  import javax.naming.ldap.LdapContext;  
  
  
  public class LdapContextCreation {  
      public static void main(String[] args) {  
          LdapContextCreation ldapContxCrtn = new LdapContextCreation();  
          LdapContext ctx = ldapContxCrtn.getLdapContext();  
      }  
      public LdapContext getLdapContext(){  
          LdapContext ctx = null;  
          try{  
              Hashtable env = new Hashtable();  
              env.put(Context.INITIAL_CONTEXT_FACTORY,  "com.sun.jndi.ldap.LdapCtxFactory");  
              env.put(Context.SECURITY_AUTHENTICATION, "Simple");  
              //it can be &lt;domain\\userid&gt; something that you use for windows login  
              //it can also be  
              env.put(Context.SECURITY_PRINCIPAL, "username@domain.com");  
              env.put(Context.SECURITY_CREDENTIALS, "password");  
              //in following property we specify ldap protocol and connection url.  
              //generally the port is 389  
              env.put(Context.PROVIDER_URL, "ldap://server.domain.com");  
              ctx = new InitialLdapContext(env, null);  
              System.out.println("Connection Successful.");  
          }catch(NamingException nex){  
              System.out.println("LDAP Connection: FAILED");  
              nex.printStackTrace();  
          }  
          return ctx;  
      }  
  
  }
  </code></pre>
  
  <p>Getting following exception:</p>
  
  <pre>
  LDAP Connection: FAILED
  javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]
      at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3053)
      at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2999)
      at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2801)
      at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2715)
      at com.sun.jndi.ldap.LdapCtx.(LdapCtx.java:305)
      at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:187)
      at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:205)
      at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:148)
      at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:78)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:235)
      at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:318)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:348)
      at javax.naming.InitialContext.internalInit(InitialContext.java:286)
      at javax.naming.InitialContext.init(InitialContext.java:308)
      at javax.naming.ldap.InitialLdapContext.(InitialLdapContext.java:99)
      at LdapContextCreation.getLdapContext(LdapContextCreation.java:27)
      at LdapContextCreation.main(LdapContextCreation.java:12)
  </pre>
  
  <p>Few more points to consider:</p>
  
  <ol>
  <li><p>Earlier I was using <code>tomcat 5.3.5</code> but somebody told me that only tomcat 6 supports it so I downloaded <code>tomcat 6.0.35</code> and currently using this version only.</p></li>
  <li><p>Configured <code>server.xml</code> and added the following code -</p>
  
  <pre><code>&lt;Realm className="org.apache.catalina.realm.JNDIRealm" 
                     debug="99" 
                     connectionURL="ldap://server.domain.com:389/"  
                     userPattern="{0}" /&gt;
  </code></pre></li>
  <li><p>Commented the following code from <code>server.xml</code> -</p>
  
  <pre><code>&lt;!-- Commenting for LDAP
    &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"
       resourceName="UserDatabase"/&gt; --&gt;
  </code></pre></li>
  <li><p>Steps 2 and 3 from <a href="http://viralpatel.net/blogs/implement-ldap-authentication-in-tomcat-jboss-server-for-java-app/" rel="noreferrer">article</a> </p></li>
  <li><p>Someone suggested that there are some jar files that are supposed to be copied to tomcat in order to run <code>ldap</code> authentication, is that something I need to do? And which <code>jar</code> files?</p></li>
  <li><p>Also, I am using the correct credentials for sure, then what is causing this issue?</p></li>
  <li><p>Is there a way I can figure out the correct attributes for LDAP in case I am using incorrect ones?</p></li>
  </ol>