title: JAAS configuration and LDAP login module in JavaEE 6
body: <p>I'm writing a question here because I haven't been able to find the solution myself for months. My situation: I have a client-server application written on java which uses Java2ee 6 and EJB3.0. The server side is deployed on the glassfish 3.0. I need to develop/implement the login module for application. Authentification must be done using ldap server and authorisation will be handled inside application. Therefore I want to hire JAAS technology to mix authentification and authorisation. I'm doing it for example like <a href="https://stackoverflow.com/questions/8414322/jaas-additional-loginmodules">here</a>. Then I follow <a href="http://www.devx.com/security/Article/28849/0/page/3" rel="nofollow noreferrer">this tutorial</a> and <a href="http://docs.oracle.com/javase/6/docs/jre/api/security/jaas/spec/com/sun/security/auth/module/LdapLoginModule.html" rel="nofollow noreferrer">official documentation</a> to perform login. My problem is that ldap login doesn't work.</p>
  
  <p>My code:</p>
  
  <pre><code>    LoginContext lc = null;
  
      try {
          CallbackHandler handler = new CallbackHandler() {
              public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
                  for( int i = 0; i &lt; callbacks.length; i++ ) {
                      if( callbacks[i] instanceof NameCallback ) {
                          // prompt the user for a username
                            NameCallback nc = (NameCallback)callbacks[i];
                            nc.setName("admin");
                            System.out.println("Login done.");
                      } else if( callbacks[i] instanceof PasswordCallback ) {
                          // prompt the user for sensitive information
                            PasswordCallback pc = (PasswordCallback)callbacks[i];
                            pc.setPassword("mypassword".toCharArray());
                            System.out.println("Password done.");
                      } else {
                          throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
                      } //end if/else
                  } //end for()
              }
          };
  
          lc = new LoginContext("myAuth", handler);
          lc.login();
          Subject subject = lc.getSubject();
      } catch (LoginException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>My JAAS configuration file:</p>
  
  <pre><code>myAuth {
  com.sun.enterprise.security.auth.login.LDAPLoginModule REQUIRED
  userProvider="ldap://mydomain:389/OU=users,DC=my,DC=domain,DC=com"
  authIdentity="{USERNAME}"
      useSSL=false
  debug=true;
  };
  </code></pre>
  
  <p>The client part of application is run with the following jvm options:</p>
  
  <pre><code>-Djava.security.auth.login.config=./jaas.conf  -Dorg.omg.CORBA.ORBInitialHost=localhost
  </code></pre>
  
  <p>On the glassfish site I set the jvm properties</p>
  
  <pre><code>-Djava.security.auth.login.config=${com.sun.aas.instanceRoot}/config/login.conf
  -Djava.naming.referral=follow
  </code></pre>
  
  <p>The login.conf file on the glassfish side contains the following lines (ADRealm is the default realm of my glassfish)</p>
  
  <pre><code>ADRealm {
  com.sun.enterprise.security.auth.login.LDAPLoginModule REQUIRED;
  };
  </code></pre>
  
  <p>Settings for ADRealm:</p>
  
  <pre><code>      &lt;property name="jaas-context" value="ldapRealm" /&gt;
        &lt;property name="base-dn" value="CN=users,DC=my,DC=domain,DC=com" /&gt;
        &lt;property name="directory" value="ldap://mydomain:3268" /&gt;
        &lt;property name="search-bind-password" value="mypassword" /&gt;
        &lt;property name="search-bind-dn" value="admin@my.domain.com" /&gt;
  </code></pre>
  
  <p>I want to stress your attention that I'm trying to perform ldap login at least to be sure that it works.</p>
  
  <p>When I run the client I get the following error:</p>
  
  <pre><code>Mar 1, 2013 1:36:44 PM com.sun.appserv.security.AppservPasswordLoginModule extractCredentials
  SEVERE: passwordlm.nopwdcred
  javax.security.auth.login.LoginException: No credentials.
  </code></pre>
  
  <p>What is strange that is worked once(!), i.e. I could obtain <code>subject</code> from <code>lc.getSubject()</code> method. Also I assume that <code>handle()</code> method above is not invoked since I don't see</p>
  
  <pre><code>Login done.
  Password done.
  </code></pre>
  
  <p>in the output. </p>
  
  <p>Please could anybody help me???</p>