java 过滤器

来源:互联网 发布:欧洲哪个国家好玩 知乎 编辑:程序博客网 时间:2024/04/30 04:09
  1. java 代码
  2. 一、使浏览器不缓存页面的过滤器     
  3. import javax.servlet.*;    
  4. import javax.servlet.http.HttpServletResponse;    
  5. import java.io.IOException;    
  6.    
  7. /**   
  8. * 用于的使 Browser 不缓存页面的过滤器   
  9. */   
  10. public class ForceNoCacheFilter implements Filter {    
  11.    
  12. public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException    
  13. {    
  14.     ((HttpServletResponse) response).setHeader("Cache-Control","no-cache");    
  15.     ((HttpServletResponse) response).setHeader("Pragma","no-cache");    
  16.     ((HttpServletResponse) response).setDateHeader ("Expires", -1);    
  17.     filterChain.doFilter(request, response);    
  18. }    
  19.    
  20. public void destroy()    
  21. {    
  22. }    
  23.    
  24.       public void init(FilterConfig filterConfig) throws ServletException    
  25. {    
  26. }    
  27. }    
  28.    
  29. 二、检测用户是否登陆的过滤器    
  30.    
  31. import javax.servlet.*;    
  32. import javax.servlet.http.HttpServletRequest;    
  33. import javax.servlet.http.HttpServletResponse;    
  34. import javax.servlet.http.HttpSession;    
  35. import java.util.List;    
  36. import java.util.ArrayList;    
  37. import java.util.StringTokenizer;    
  38. import java.io.IOException;    
  39.    
  40. /**   
  41. * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面    
  42.   
  43.   
  44. * 配置参数    
  45.   
  46.   
  47. * checkSessionKey 需检查的在 Session 中保存的关键字   
  48.   
  49. * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath   
  50.   
  51. * notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath   
  52.   
  53. */   
  54. public class CheckLoginFilter    
  55. implements Filter    
  56. {    
  57.       protected FilterConfig filterConfig = null;    
  58.       private String redirectURL = null;    
  59.       private List notCheckURLList = new ArrayList();    
  60.       private String sessionKey = null;    
  61.    
  62. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException    
  63. {    
  64.     HttpServletRequest request = (HttpServletRequest) servletRequest;    
  65.     HttpServletResponse response = (HttpServletResponse) servletResponse;    
  66.    
  67.      HttpSession session = request.getSession();    
  68.     if(sessionKey == null)    
  69.     {    
  70.      filterChain.doFilter(request, response);    
  71.      return;    
  72.     }    
  73.     if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)    
  74.     {    
  75.      response.sendRedirect(request.getContextPath() + redirectURL);    
  76.      return;    
  77.     }    
  78.     filterChain.doFilter(servletRequest, servletResponse);    
  79. }    
  80.    
  81. public void destroy()    
  82. {    
  83.     notCheckURLList.clear();    
  84. }    
  85.    
  86. private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)    
  87. {    
  88.     String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());    
  89.     return notCheckURLList.contains(uri);    
  90. }    
  91.    
  92. public void init(FilterConfig filterConfig) throws ServletException    
  93. {    
  94.     this.filterConfig = filterConfig;    
  95.     redirectURL = filterConfig.getInitParameter("redirectURL");    
  96.     sessionKey = filterConfig.getInitParameter("checkSessionKey");    
  97.    
  98.     String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");    
  99.    
  100.     if(notCheckURLListStr != null)    
  101.     {    
  102.      StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");    
  103.      notCheckURLList.clear();    
  104.      while(st.hasMoreTokens())    
  105.      {    
  106.       notCheckURLList.add(st.nextToken());    
  107.      }    
  108.     }    
  109. }    
  110. }    
  111.    
  112. 三、字符编码的过滤器    
  113.    
  114. import javax.servlet.*;    
  115. import java.io.IOException;    
  116.    
  117. /**   
  118. * 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题   
  119. */   
  120. public class CharacterEncodingFilter    
  121. implements Filter    
  122. {    
  123. protected FilterConfig filterConfig = null;    
  124. protected String encoding = "";    
  125.    
  126. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException    
  127. {    
  128.           if(encoding != null)    
  129.            servletRequest.setCharacterEncoding(encoding);    
  130.           filterChain.doFilter(servletRequest, servletResponse);    
  131. }    
  132.    
  133. public void destroy()    
  134. {    
  135.     filterConfig = null;    
  136.     encoding = null;    
  137. }    
  138.    
  139.       public void init(FilterConfig filterConfig) throws ServletException    
  140. {    
  141.            this.filterConfig = filterConfig;    
  142.           this.encoding = filterConfig.getInitParameter("encoding");    
  143.    
  144. }    
  145. }    
  146.    
  147. 四、资源保护过滤器    
  148.    
  149.    
  150. package catalog.view.util;    
  151.    
  152. import javax.servlet.Filter;    
  153. import javax.servlet.FilterConfig;    
  154. import javax.servlet.ServletRequest;    
  155. import javax.servlet.ServletResponse;    
  156. import javax.servlet.FilterChain;    
  157. import javax.servlet.ServletException;    
  158. import javax.servlet.http.HttpServletRequest;    
  159. import java.io.IOException;    
  160. import java.util.Iterator;    
  161. import java.util.Set;    
  162. import java.util.HashSet;    
  163. //    
  164. import org.apache.commons.logging.Log;    
  165. import org.apache.commons.logging.LogFactory;    
  166.    
  167. /**   
  168. * This Filter class handle the security of the application.   
  169. *    
  170. * It should be configured inside the web.xml.   
  171. *    
  172. * @author Derek Y. Shen   
  173. */   
  174. public class SecurityFilter implements Filter {    
  175. //the login page uri    
  176. private static final String LOGIN_PAGE_URI = "login.jsf";    
  177.      
  178. //the logger object    
  179. private Log logger = LogFactory.getLog(this.getClass());    
  180.      
  181. //a set of restricted resources    
  182. private Set restrictedResources;    
  183.      
  184. /**   
  185.    * Initializes the Filter.   
  186.    */   
  187. public void init(FilterConfig filterConfig) throws ServletException {    
  188.    this.restrictedResources = new HashSet();    
  189.    this.restrictedResources.add("/createProduct.jsf");    
  190.    this.restrictedResources.add("/editProduct.jsf");    
  191.    this.restrictedResources.add("/productList.jsf");    
  192. }    
  193.      
  194. /**   
  195.    * Standard doFilter object.   
  196.    */   
  197. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)    
  198.     throws IOException, ServletException {    
  199.    this.logger.debug("doFilter");    
  200.       
  201.    String contextPath = ((HttpServletRequest)req).getContextPath();    
  202.    String requestUri = ((HttpServletRequest)req).getRequestURI();    
  203.       
  204.    this.logger.debug("contextPath = " + contextPath);    
  205.    this.logger.debug("requestUri = " + requestUri);    
  206.       
  207.    if (this.contains(requestUri, contextPath) && !this.authorize((HttpServletRequest)req)) {    
  208.     this.logger.debug("authorization failed");    
  209.     ((HttpServletRequest)req).getRequestDispatcher(LOGIN_PAGE_URI).forward(req, res);    
  210.    }    
  211.    else {    
  212.     this.logger.debug("authorization succeeded");    
  213.     chain.doFilter(req, res);    
  214.    }    
  215. }    
  216.      
  217. public void destroy() {}     
  218.      
  219. private boolean contains(String value, String contextPath) {    
  220.    Iterator ite = this.restrictedResources.iterator();    
  221.       
  222.    while (ite.hasNext()) {    
  223.     String restrictedResource = (String)ite.next();    
  224.        
  225.     if ((contextPath + restrictedResource).equalsIgnoreCase(value)) {    
  226.      return true;    
  227.     }    
  228.    }    
  229.       
  230.    return false;    
  231. }    
  232.      
  233. private boolean authorize(HttpServletRequest req) {    
  234.    
  235.                //处理用户登录    
  236.         /* UserBean user = (UserBean)req.getSession().getAttribute(BeanNames.USER_BEAN);   
  237.      
  238.    if (user != null && user.getLoggedIn()) {   
  239.     //user logged in   
  240.     return true;   
  241.    }   
  242.    else {   
  243.     return false;   
  244.    }*/   
  245. }    
  246. }    
原创粉丝点击