📄 signonfilter.java
字号:
package com.ebusiness.ebank.security;/** * <p>Title: </p> * <p>Description: This class manage user's front-end sign on process. If a user * has not authenticated and try to access to the web resoures(jsp and actions) * , the user will be redirected to a loggedout pag on which there is a * link to allow the user to login. Once the user is * authenticated, the user can access eBank system based on it's * role-based security policy. Logout will release the session</p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: eBusiness Inc., All right reserved</p> * @author unascribed * @version 1.0 */import java.io.IOException;import java.util.Properties;import java.util.Iterator;import java.util.Enumeration;import java.util.Set;import java.util.Map;import java.util.HashSet;import java.util.HashMap;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.http.HttpSession;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.log4j.MDC;import com.ebusiness.ebank.util.Constants;import com.ebusiness.ebank.exception.SystemException;import weblogic.servlet.security.ServletAuthentication;import com.ebusiness.ebank.bean.*;import java.util.Date;import java.util.Locale;import java.sql.Timestamp;public class SignOnFilter implements Filter{ private Logger log = Logger.getLogger(this.getClass()); private FilterConfig config = null; private String errorLoginPage = null; private String loginPage = null; private String welcomePage = null; private String host = null; private String loggedoutPage = null; private String alreadyLoggedinPage = null; public void init(FilterConfig config) throws ServletException { this.config = config; errorLoginPage = config.getInitParameter("errorLogin-page"); loginPage = config.getInitParameter("login-page"); welcomePage = config.getInitParameter("welcome-page"); loggedoutPage = config.getInitParameter("loggedout-page"); alreadyLoggedinPage = config.getInitParameter("alreadyLoggedin-Page"); securityConfig(); log.info("eBank SignOnFilter was initialized"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hreq = (HttpServletRequest)request; String targetURL = hreq.getServletPath(); log.info("TargetURL: " + targetURL); /** if ("/jsp/signon/login.jsp".equals(targetURL)) { chain.doFilter(request,response); return; } */ String userID = (String)hreq.getRemoteUser(); log.debug("TargetURL: " + targetURL); if (host == null) { String st = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); log.info("URL: " + st); host = st; } if (userID != null) //The user is logged in, so allow the user through { if ("/login.do".equals(targetURL)) { request.getRequestDispatcher(alreadyLoggedinPage).forward(request, response); return; } else if ("/logout.do".equals(targetURL)) { HttpSession session = hreq.getSession(); session.invalidate(); request.getRequestDispatcher(loginPage).forward(request, response); return; } chain.doFilter(request,response); } else if ("/login.do".equals(targetURL)) chain.doFilter(request,response); else //The user's session was timeout or the user hasn't been logged-in yet request.getRequestDispatcher(loggedoutPage).forward(request, response); return; } public void destroy() { //do nothing } //load securityConfig properties and initialize FunctionConstraint and ColumnConstraint private void securityConfig() { //load securityConfig properties and initialize FunctionConstraint and ColumnConstraint Properties prop = new Properties(); try { prop.load(this.getClass().getResourceAsStream("securityConfig.properties")); } catch (Exception e) { log.fatal("securityConfig.properties file does not found", e); } Enumeration props = prop.propertyNames(); String key = ""; Set roles = new HashSet(); Map funcPolicies = new HashMap(); Map funcIndicators = new HashMap(); Map colPolicies = new HashMap(); Map colIndicators = new HashMap(); while(props.hasMoreElements()) { key = (String)props.nextElement(); if (key.indexOf("restricted") > 0 || key.indexOf("authorized") > 0) { String roleNameKey = key.substring(0, key.indexOf('.')); String roleName = prop.getProperty(roleNameKey); roles.add(roleName); if (key.indexOf("function") > 0) //function restrictions { Set functions = (Set)funcPolicies.get(roleName); if (functions == null) functions = new HashSet(); functions.add(prop.getProperty(key)); funcPolicies.put(roleName, functions); if (key.indexOf("restricted") > 0) funcIndicators.put(roleName, "restricted"); else if (key.indexOf("authorized") > 0) funcIndicators.put(roleName, "authorized"); } else if (key.indexOf("column") > 0) //column restrictions { Set columns = (Set)colPolicies.get(roleName); if (columns == null) columns = new HashSet(); columns.add(prop.getProperty(key)); colPolicies.put(roleName, columns); if (key.indexOf("restricted") > 0) colIndicators.put(roleName, "restricted"); else if (key.indexOf("authorized") > 0) colIndicators.put(roleName, "authorized"); } } } log.info("securityConfig properties file was successfully loaded."); //Initialize eBank roles and refresh interval for UserContainer UserContainer.initEbankRoles((String[])roles.toArray(new String[roles.size()])); String refreshInterval = prop.getProperty("refreshInterval"); if (refreshInterval != null) { long interval = Long.parseLong(refreshInterval); if (interval > 0) UserContainer.initRefreshInterval(interval); } //Initialize FunctionConstraint FunctionConstraint.init(funcPolicies, funcIndicators); //Initialize ColumnConstraint ColumnConstraint.init(colPolicies, colIndicators); Iterator i = roles.iterator(); log.info("There are following roles defined in eBank application: "); while (i.hasNext()) { log.info((String)i.next()); } i = funcPolicies.keySet().iterator(); log.info("******Function Restrictions****** "); while (i.hasNext()) { key = (String)i.next(); log.info("Restricted Functions for Role " + key + ": "); Set functions = (Set)funcPolicies.get(key); Iterator iter = functions.iterator(); while (iter.hasNext()) { log.info((String)iter.next()); } } i = colPolicies.keySet().iterator(); log.info("******Column Restrictions****** "); while (i.hasNext()) { key = (String)i.next(); log.info("Restricted Columns for Role " + key + ": "); Set columns = (Set)colPolicies.get(key); Iterator iter = columns.iterator(); while (iter.hasNext()) { log.info((String)iter.next()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -