Wednesday, August 22, 2007

Tomcat, JAAS and config file

As you know Tomcat is able to use JAAS Login Module for Realm authentication. But if you use standard implementation you should start Tomcat with parameter -Djava.security.auth.login.config==$CATALINA_HOME/conf/jaas.config, where jaas.config is your JAAS configuration file.

So I extended JAASRealm with jaasConfigFile attribute.

package my.realm;

import org.apache.catalina.realm.JAASRealm;

/**
 * This is customization of JAASRealm, created for taking ability of setting JAAS Config File via realm properties.
 * @author Sergey Vinogradov
 */
public class GDJaasRealm extends JAASRealm {
    private String jaasConfigFile;

    public void init() {
        System.setProperty("java.security.auth.login.config", jaasConfigFile);
        super.init();

    }

    public String getJaasConfigFile() {
        return jaasConfigFile;
    }

    public void setJaasConfigFile(String jaasConfigFile) {
        this.jaasConfigFile = jaasConfigFile;
    }
}

Thus you may declare this Realm at server.xml instead of org.apache.catalina.realm.JAASRealm
<Realm className="my.realm.GDJaasRealm"                 
          userClassNames="my.ldap.UserPrincipal"
          roleClassNames="my.ldap.RolePrincipal"       
          appName="my_app"
          jaasConfigFile="D:\ldap-jaas.conf"/>

1 comment:

Mohsin Khan said...

Do you have sample application for this