title: Custom Glassfish Security Realm does not work (unable to find LoginModule)
body: <p>I'm trying to get a Custom Security Realm in Glassfish working (i tried 3.0.1 final and 3.1 B33). I read nearly all tutorials about this, but it doesn't not work on my System. I'm getting the error </p>
  
  <pre><code>Login failed: javax.security.auth.login.LoginException: unable to find LoginModule class: com.company.security.realm.CustomLoginModule
  </code></pre>
  
  <p>when trying to login.</p>
  
  <p>Here is what i did:
  I created a little Maven project, which contains the needed Realm class, CustomRealm, and the corresponding LoginModule, CustomLoginModule. 
  My pom.xml:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
    &lt;groupId&gt;com.mycompany&lt;/groupId&gt;
    &lt;artifactId&gt;security.realm&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
  
    &lt;name&gt;Custom JDBCRealm&lt;/name&gt;
    &lt;url&gt;http://maven.apache.org&lt;/url&gt;
  
    &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;/properties&gt;
  
    &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.security&lt;/groupId&gt;
          &lt;artifactId&gt;security&lt;/artifactId&gt;
          &lt;version&gt;3.1-b33&lt;/version&gt;
      &lt;/dependency&gt;
    &lt;/dependencies&gt;
  
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.6&lt;/source&gt;
                  &lt;target&gt;1.6&lt;/target&gt;
                  &lt;optimise&gt;true&lt;/optimise&gt;
                  &lt;debug&gt;true&lt;/debug&gt;
                  &lt;encoding&gt;UTF-8&lt;/encoding&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;    
        &lt;/plugins&gt;
    &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>My Custom Realm class:</p>
  
  <pre><code>package com.company.security.realm;
  
  import com.sun.appserv.security.AppservRealm;
  import com.sun.enterprise.security.auth.realm.BadRealmException;
  import com.sun.enterprise.security.auth.realm.InvalidOperationException;
  import com.sun.enterprise.security.auth.realm.NoSuchRealmException;
  import com.sun.enterprise.security.auth.realm.NoSuchUserException;
  import java.util.Enumeration;
  import java.util.Properties;
  import java.util.Vector;
  
  public class CustomRealm extends AppservRealm
  {
      Vector&lt;String&gt; groups = new Vector&lt;String&gt;();
  
      private String jaasCtxName;
  
      private String startWith;
  
      @Override
      public void init(Properties properties)
      throws BadRealmException, NoSuchRealmException {
          jaasCtxName = properties.getProperty("jaas-context", "customRealm");
          startWith = properties.getProperty("startWith", "z");
          groups.add("dummy");
      }
  
      @Override
      public String getAuthType()
      {
          return "Custom Realm";
      }
  
      public String[] authenticate(String username, char[] password) 
      {
          // if (isValidLogin(username, password))
          return (String[]) groups.toArray();
      }
  
      @Override
      public Enumeration getGroupNames(String username) 
      throws InvalidOperationException, NoSuchUserException 
      {
          return groups.elements();
      }
  
      @Override 
      public String getJAASContext()
      {
          return jaasCtxName;
      }
  
      public String getStartWith()
      {
          return startWith;
      }
  }
  </code></pre>
  
  <p>My LoginModule class:</p>
  
  <pre><code>package com.company.security.realm;
  
  import com.sun.appserv.security.AppservPasswordLoginModule;
  import com.sun.enterprise.security.auth.login.common.LoginException;
  import java.util.Set;
  import org.glassfish.security.common.PrincipalImpl;
  
  public class CustomLoginModule extends AppservPasswordLoginModule
  {
      @Override
      protected void authenticateUser() throws LoginException
      {
          _logger.info("CustomRealm : authenticateUser for " +  _username);
          final CustomRealm realm = (CustomRealm)_currentRealm;
  
          if ( (_username == null) || (_username.length() == 0) || !_username.startsWith(realm.getStartWith())) 
              throw new LoginException("Invalid credentials");
  
          String[] grpList = realm.authenticate(_username, getPasswordChar()); 
          if (grpList == null) { 
              throw new LoginException("User not in groups");
          }
  
          _logger.info("CustomRealm : authenticateUser for " +  _username);
  
          Set principals = _subject.getPrincipals();
          principals.add(new PrincipalImpl(_username));
  
          this.commitUserAuthentication(grpList);
  
      }
  
  }
  </code></pre>
  
  <p>I compiled this Maven project and copyied the resulting JAR-file to the Glassfish/lib directory. Then i added the Security Realm "customRealm" to my Glassfish with asadmin:</p>
  
  <pre><code>asadmin create-auth-realm 
    --classname com.company.security.realm.CustomRealm 
    --property jaas-context=customRealm:startWith=a customRealm
  </code></pre>
  
  <p>I also referenced the LoginModule class for the JAAS context of my Custom Realm, therefore i inserted this into the login.conf of my domain:</p>
  
  <pre><code>customRealm {
    com.company.security.realm.CustomLoginModule required;
  };
  </code></pre>
  
  <p>Although this LoginModule SHOULD BE on the Glassfish classpath, as it's classfile is packaged in the JAR that i put into the Glassfish/lib-dir, it cannot be found when i try to login. For login, i build a simple JSF-project, which calls the HttpServletRequest-login-method of Servlet 3.0. 
  When trying to login i'm getting the following Exception:</p>
  
  <pre><code>2010-12-24T14:41:31.613+0100|WARNING|glassfish3.0.1|
  javax.enterprise.system.container.web.com.sun.web.security|_ThreadID=25;
  _ThreadName=Thread-1;|Web login failed: Login failed: 
  javax.security.auth.login.LoginException: unable to find LoginModule class:
  com.company.security.realm.CustomLoginModule
  </code></pre>
  
  <p>Anybody got an idea what i can do that Glassfish loads the LoginModule-class?</p>