--- C:/Users/Utente/AppData/Local/Temp/JAASRealm.java-revBASE.svn000.tmp.java dom ago 26 17:47:07 2012 +++ C:/work/tomcat7/java/org/apache/catalina/realm/JAASRealm.java dom ago 26 17:46:24 2012 @@ -19,6 +19,11 @@ package org.apache.catalina.realm; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.security.Principal; import java.util.ArrayList; import java.util.Iterator; @@ -27,6 +32,7 @@ import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.AccountExpiredException; +import javax.security.auth.login.Configuration; import javax.security.auth.login.CredentialExpiredException; import javax.security.auth.login.FailedLoginException; import javax.security.auth.login.LoginContext; @@ -122,7 +128,7 @@ * * @author Craig R. McClanahan * @author Yoav Shapira - * @version $Id: JAASRealm.java 1361770 2012-07-15 19:38:51Z markt $ + * @version $Id$ */ public class JAASRealm @@ -173,8 +179,29 @@ protected boolean useContextClassLoader = true; + + /** + * Path to find a JAAS configuration file, if not set global JVM JAAS configuraion will be used + */ + protected String configfile; + // ------------------------------------------------------------- Properties + /** + * getter for the configfile member variable + */ + public String getConfigfile() { + return configfile; + } + + /** + * setter for the configfile member variable + */ + public void setConfigfile(String configfile) { + this.configfile = configfile; + } + + /** * setter for the appName member variable @@ -389,7 +416,8 @@ } try { - loginContext = new LoginContext(appName, callbackHandler); + Configuration config = getConfig(); + loginContext = new LoginContext(appName, null, callbackHandler, config); } catch (Throwable e) { ExceptionUtils.handleThrowable(e); log.error(sm.getString("jaasRealm.unexpectedError"), e); @@ -605,4 +633,48 @@ super.startInternal(); } + protected Configuration jaasConfiguration; + protected volatile boolean jaasConfigurationLoaded = false; + + /** + * Load custom JAAS Configuration + */ + protected Configuration getConfig() { + try { + if (jaasConfigurationLoaded) { + return jaasConfiguration; + } + synchronized (this) { + if (configfile == null) { + jaasConfigurationLoaded = true; + return null; + } + URL resource = Thread.currentThread().getContextClassLoader().getResource(configfile); + URI uri = resource.toURI(); + Class sunConfigFile = Class.forName("com.sun.security.auth.login.ConfigFile"); + Constructor constructor = sunConfigFile.getConstructor(URI.class); + Configuration config = constructor.newInstance(uri); + this.jaasConfiguration = config; + this.jaasConfigurationLoaded = true; + return this.jaasConfiguration; + } + } catch (URISyntaxException ex) { + throw new RuntimeException(ex); + } catch (NoSuchMethodException ex) { + throw new RuntimeException(ex); + } catch (SecurityException ex) { + throw new RuntimeException(ex); + } catch (InstantiationException ex) { + throw new RuntimeException(ex); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } catch (IllegalArgumentException ex) { + throw new RuntimeException(ex); + } catch (InvocationTargetException ex) { + throw new RuntimeException(ex.getCause()); + } catch (ClassNotFoundException ex) { + throw new RuntimeException(ex); + } + + } }