www.pudn.com > SecurityFilter.rar > SecurityConfig.java


package dev.trade.common.securityfilter.config; 
 
import java.io.*; 
import java.net.*; 
import java.util.*; 
import org.apache.commons.digester.*; 
import org.xml.sax.*; 
import dev.trade.common.securityfilter.authenticator.Authenticator; 
 
/** 
 * 

Title: 权限过滤器

* *

Description: SecurityConfig的配置信息(security-config.xml文件)

* *

Copyright: Copyright (c) 2006

* *

Company:

* * @author Zheng YanNan * @version 1.0 */ public class SecurityConfig{ private String loginPage = null; private String loginSubmitPage = null; private String loginErrorPage = null; private String authErrorPage = null; private String logoutPage = null; private String defaultPage = null; private ArrayList securityConstraints = null; private Authenticator authenticator = null; private boolean validating; private String authMethod; // private SecurityRealmInterface realm = null; // private Object lastRealm = null; // private String realmName; /** * Constructor that takes the validating flag and debug level to be used while parsing. * * @param validating validate the input file, true = validate, false = don't validate */ public SecurityConfig(boolean validating) { this.validating = false; //validating; } /** * Return the login page URL. */ public String getLoginPage() { return loginPage; } /** * Set the login page URL. This is the page the user will be sent to to log in (i.e. the login form). * * @param loginPage The login page url (relative to site root) */ public void setLoginPage(String loginPage) { this.loginPage = loginPage; } public String getLoginSubmitPage(){ return loginSubmitPage; } public void setLoginSubmitPage(String loginSubmitPage){ this.loginSubmitPage = loginSubmitPage; } /** * Return the error page URL. */ public String getLoginErrorPage() { return loginErrorPage; } /** * Set the error page URL. This is the page the user will be sent to if login request fails. * * @param errorPage The login page URL (relative to site root) */ public void setLoginErrorPage(String loginErrorPage) { this.loginErrorPage = loginErrorPage; } public String getAuthErrorPage(){ return authErrorPage; } public void setAuthErrorPage(String authErrorPage){ this.authErrorPage = authErrorPage; } /** * Return the logout page URL. */ public String getLogoutPage() { return logoutPage; } /** * Set the logout page URL. * * @param logoutPage The logout page url (relative to site root) */ public void setLogoutPage(String logoutPage) { this.logoutPage = logoutPage; } /** * Return the default page URL. */ public String getDefaultPage() { return defaultPage; } /** * Set the default page URL. This is the page the user will be sent to if they submit a login request without * being forced to the login page by the filter. * * @param defaultPage The default page URL (relative to site root) */ public void setDefaultPage(String defaultPage) { this.defaultPage = defaultPage; } /** * Get the authentication method being used to challenge the user. * Currently, only BASIC and FORM based are supported. * * @return BASIC or FORM */ public String getAuthMethod() { return authMethod; } /** * Set the authentication method being used to challenge the user. * Currently, only BASIC and FORM based are supported. * * @param authMethod The authentication method to be used by the filter */ public void setAuthMethod(String authMethod) { this.authMethod = authMethod; } public Authenticator getAuthenticator(){ return authenticator; } public synchronized void setAuthenticator(Authenticator auth) { this.authenticator = auth; } /** * Get the authentication realm name. * This is used for BASIC authentication. * * @return the realm-name configured by the application developer */ // public String getRealmName() { // return realmName; // } /** * Set the authentication realm name. * This is used for BASIC authentication. * * @param realmName the realm name to be used for BASIC authentication */ // public void setRealmName(String realmName) { // this.realmName = realmName; // } /** * Return the realm to use for authentication. This is the outer-most realm if nested realms are used. * The outer-most realm must be listed first in the configuration file. */ // public SecurityRealmInterface getRealm() { // return realm; // } /** * Adds a realm to use for authentication. * * The first time this method is called, the realm must implement SecurityRealmInterface. * Subsequent calls can be any kind of object, and setRealm(realm) will be called on the * last realm passed to this method. This allows nesting of realms for caching or when a * realm adapter is used. * * @param realm The realm to use, or nest in deeper realm */ // public synchronized void addRealm( // Object realm // ) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { // if (this.realm == null) { // this.realm = (SecurityRealmInterface) realm; // lastRealm = realm; // } else { // // TODO: allow addRealm signaure to take types besides Object -- will commons-beanutils help? // // call lastRealm.setRealm(realm) // Method addMethod = lastRealm.getClass().getMethod("setRealm", new Class[]{Object.class}); // addMethod.invoke(lastRealm, new Object[]{realm}); // lastRealm = realm; // } // } /** * Return the configured SecurityConstraints. */ public List getSecurityConstraints() { return this.securityConstraints; } /** * Adds a SecurityConstraint. * * @param constraint The SecurityConstraint to add */ public void addSecurityConstraint(SecurityConstraint constraint) { securityConstraints.add(constraint); } /** * Loads configuration from the specifued configURL. * * @param configURL The url to load. * * @exception IOException if an input/output error occurs * @exception SAXException if the file has invalid xml syntax */ public void loadConfig(URL configURL) throws IOException, SAXException { securityConstraints = new ArrayList(); Digester digester = new Digester(); // only register the DTDs if we will be validating // registerLocalDTDs(digester); digester.push(this); digester.setUseContextClassLoader(true); digester.setValidating(false); // // realms // digester.addObjectCreate("securityfilter-config/realm", null, "className"); // digester.addSetProperty("securityfilter-config/realm/realm-param", "name", "value"); // digester.addSetNext("securityfilter-config/realm", "addRealm", "java.lang.Object"); // auth method, realm name digester.addCallMethod("securityfilter-config/auth-config/auth-method", "setAuthMethod", 0); // authenticator digester.addObjectCreate("securityfilter-config/auth-config/authenticator", null, "className"); digester.addSetProperty("securityfilter-config/auth-config/authenticator/param", "name", "value"); digester.addSetNext("securityfilter-config/auth-config/authenticator", "setAuthenticator", "dev.trade.common.securityfilter.authenticator.Authenticator"); // digester.addCallMethod("securityfilter-config/auth-config/realm-name", "setRealmName", 0); // login, error, logout, and default pages digester.addCallMethod("securityfilter-config/auth-config/page-config/login-page", "setLoginPage", 0); digester.addCallMethod("securityfilter-config/auth-config/page-config/login-submit-page", "setLoginSubmitPage", 0); digester.addCallMethod("securityfilter-config/auth-config/page-config/login-error-page", "setLoginErrorPage", 0); digester.addCallMethod("securityfilter-config/auth-config/page-config/logout-page", "setLogoutPage", 0); digester.addCallMethod("securityfilter-config/auth-config/page-config/auth-error-page", "setAuthErrorPage", 0); digester.addCallMethod( "securityfilter-config/auth-config/page-config/default-page", "setDefaultPage", 0); // persistent login manager // digester.addObjectCreate("securityfilter-config/auth-config/page-config/remember-me", null, "className"); // digester.addSetProperty( // "securityfilter-config/auth-config/page-config/remember-me/remember-me-param", // "name", // "value" // ); // digester.addSetNext( // "securityfilter-config/auth-config/page-config/remember-me", // "setPersistentLoginManager", // "dev.trade.common.securityfilter.authenticator.persistent.PersistentLoginManagerInterface" // ); // security-constraint digester.addObjectCreate( "securityfilter-config/security-constraint", "dev.trade.common.securityfilter.config.SecurityConstraint"); digester.addSetNext( "securityfilter-config/security-constraint", "addSecurityConstraint", "dev.trade.common.securityfilter.config.SecurityConstraint"); // auth-constraint digester.addObjectCreate( "securityfilter-config/security-constraint/auth-constraint", "dev.trade.common.securityfilter.config.AuthConstraint"); digester.addSetNext( "securityfilter-config/security-constraint/auth-constraint", "setAuthConstraint", "dev.trade.common.securityfilter.config.AuthConstraint"); digester.addCallMethod( "securityfilter-config/security-constraint/auth-constraint/role-name", "addRole", 0); // web-resource-collection digester.addObjectCreate( "securityfilter-config/security-constraint/web-resource-collection", "dev.trade.common.securityfilter.config.WebResourceCollection"); digester.addSetNext( "securityfilter-config/security-constraint/web-resource-collection", "addWebResourceCollection", "dev.trade.common.securityfilter.config.WebResourceCollection"); digester.addCallMethod( "securityfilter-config/security-constraint/web-resource-collection/web-resource-name", "setWebResourceName", 0); digester.addCallMethod( "securityfilter-config/security-constraint/web-resource-collection/url-pattern", "addURLPattern", 0); digester.addCallMethod( "securityfilter-config/security-constraint/web-resource-collection/http-method", "addHttpMethod", 0); InputSource input = new InputSource(configURL.openStream()); digester.parse(input); } /** * 注册DTD * @param digester */ // protected void registerLocalDTDs(Digester digester) { // // register the local version of the 1.0 DTD, if it is available // URL dtd1_0 = this.getClass().getResource("/org/securityfilter/resources/securityfilter-config_1_0.dtd"); // if (dtd1_0 != null) { // digester.register("-//SecurityFilter.org//DTD Security Filter Configuration 1.0//EN", dtd1_0.toString()); // } // // // register the local version of the 1.1 DTD, if it is available // URL dtd1_1 = this.getClass().getResource("/org/securityfilter/resources/securityfilter-config_1_1.dtd"); // if (dtd1_1 != null) { // digester.register("-//SecurityFilter.org//DTD Security Filter Configuration 1.1//EN", dtd1_1.toString()); // } // // // register the local version of the 2.0 DTD, if it is available // URL dtd2_0 = this.getClass().getResource("/org/securityfilter/resources/securityfilter-config_2_0.dtd"); // if (dtd2_0 != null) { // digester.register("-//SecurityFilter.org//DTD Security Filter Configuration 2.0//EN", dtd2_0.toString()); // } // } } // ------------------------------------------------------------------------ // EOF