`

四种过滤器

阅读更多

四种过滤器

一、使浏览器不缓存页面的过滤器   

二、检测用户是否登陆的过滤器 

三、字符编码的过滤器 

四、资源保护过滤器  

下面的写法可能不是很标准,但是意思是这个意思

java 代码
一、使浏览器不缓存页面的过滤器     
import javax.servlet.*;    
import javax.servlet.http.HttpServletResponse;    
import java.io.IOException;    
   
/**   
* 用于的使 Browser 不缓存页面的过滤器   
*/   
public class ForceNoCacheFilter implements Filter {    
   
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException    
{    
    ((HttpServletResponse) response).setHeader("Cache-Control","no-cache");    
    ((HttpServletResponse) response).setHeader("Pragma","no-cache");    
    ((HttpServletResponse) response).setDateHeader ("Expires", -1);    
    filterChain.doFilter(request, response);    
}    
   
public void destroy()    
{    
}    
   
      public void init(FilterConfig filterConfig) throws ServletException    
{    
}    
}    
   
二、检测用户是否登陆的过滤器    
   
import javax.servlet.*;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
import javax.servlet.http.HttpSession;    
import java.util.List;    
import java.util.ArrayList;    
import java.util.StringTokenizer;    
import java.io.IOException;    
   
/**   
* 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面    
  
  
* 配置参数    
  
  
* checkSessionKey 需检查的在 Session 中保存的关键字   
  
* redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath   
  
* notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath   
  
*/   
public class CheckLoginFilter    
implements Filter    
{    
      protected FilterConfig filterConfig = null;    
      private String redirectURL = null;    
      private List notCheckURLList = new ArrayList();    
      private String sessionKey = null;    
   
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException    
{    
    HttpServletRequest request = (HttpServletRequest) servletRequest;    
    HttpServletResponse response = (HttpServletResponse) servletResponse;    
   
     HttpSession session = request.getSession();    
    if(sessionKey == null)    
    {    
     filterChain.doFilter(request, response);    
     return;    
    }    
    if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)    
    {    
     response.sendRedirect(request.getContextPath() + redirectURL);    
     return;    
    }    
    filterChain.doFilter(servletRequest, servletResponse);    
}    
   
public void destroy()    
{    
    notCheckURLList.clear();    
}    
   
private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)    
{    
    String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());    
    return notCheckURLList.contains(uri);    
}    
   
public void init(FilterConfig filterConfig) throws ServletException    
{    
    this.filterConfig = filterConfig;    
    redirectURL = filterConfig.getInitParameter("redirectURL");    
    sessionKey = filterConfig.getInitParameter("checkSessionKey");    
   
    String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");    
   
    if(notCheckURLListStr != null)    
    {    
     StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");    
     notCheckURLList.clear();    
     while(st.hasMoreTokens())    
     {    
      notCheckURLList.add(st.nextToken());    
     }    
    }    
}    
}    
   
三、字符编码的过滤器    
   
import javax.servlet.*;    
import java.io.IOException;    
   
/**   
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题   
*/   
public class CharacterEncodingFilter    
implements Filter    
{    
protected FilterConfig filterConfig = null;    
protected String encoding = "";    
   
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException    
{    
          if(encoding != null)    
           servletRequest.setCharacterEncoding(encoding);    
          filterChain.doFilter(servletRequest, servletResponse);    
}    
   
public void destroy()    
{    
    filterConfig = null;    
    encoding = null;    
}    
   
      public void init(FilterConfig filterConfig) throws ServletException    
{    
           this.filterConfig = filterConfig;    
          this.encoding = filterConfig.getInitParameter("encoding");    
   
}    
}    
   
四、资源保护过滤器    
   
   
package catalog.view.util;    
   
import javax.servlet.Filter;    
import javax.servlet.FilterConfig;    
import javax.servlet.ServletRequest;    
import javax.servlet.ServletResponse;    
import javax.servlet.FilterChain;    
import javax.servlet.ServletException;    
import javax.servlet.http.HttpServletRequest;    
import java.io.IOException;    
import java.util.Iterator;    
import java.util.Set;    
import java.util.HashSet;    
//    
import org.apache.commons.logging.Log;    
import org.apache.commons.logging.LogFactory;    
   
/**   
* This Filter class handle the security of the application.   
*    
* It should be configured inside the web.xml.   
*    
* @author Derek Y. Shen   
*/   
public class SecurityFilter implements Filter {    
//the login page uri    
private static final String LOGIN_PAGE_URI = "login.jsf";    
     
//the logger object    
private Log logger = LogFactory.getLog(this.getClass());    
     
//a set of restricted resources    
private Set restrictedResources;    
     
/**   
   * Initializes the Filter.   
   */   
public void init(FilterConfig filterConfig) throws ServletException {    
   this.restrictedResources = new HashSet();    
   this.restrictedResources.add("/createProduct.jsf");    
   this.restrictedResources.add("/editProduct.jsf");    
   this.restrictedResources.add("/productList.jsf");    
}    
     
/**   
   * Standard doFilter object.   
   */   
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)    
    throws IOException, ServletException {    
   this.logger.debug("doFilter");    
      
   String contextPath = ((HttpServletRequest)req).getContextPath();    
   String requestUri = ((HttpServletRequest)req).getRequestURI();    
      
   this.logger.debug("contextPath = " + contextPath);    
   this.logger.debug("requestUri = " + requestUri);    
      
   if (this.contains(requestUri, contextPath) && !this.authorize((HttpServletRequest)req)) {    
    this.logger.debug("authorization failed");    
    ((HttpServletRequest)req).getRequestDispatcher(LOGIN_PAGE_URI).forward(req, res);    
   }    
   else {    
    this.logger.debug("authorization succeeded");    
    chain.doFilter(req, res);    
   }    
}    
     
public void destroy() {}     
     
private boolean contains(String value, String contextPath) {    
   Iterator ite = this.restrictedResources.iterator();    
      
   while (ite.hasNext()) {    
    String restrictedResource = (String)ite.next();    
       
    if ((contextPath + restrictedResource).equalsIgnoreCase(value)) {    
     return true;    
    }    
   }    
      
   return false;    
}    
     
private boolean authorize(HttpServletRequest req) {    
   
               //处理用户登录    
        /* UserBean user = (UserBean)req.getSession().getAttribute(BeanNames.USER_BEAN);   
     
   if (user != null && user.getLoggedIn()) {   
    //user logged in   
    return true;   
   }   
   else {   
    return false;   
   }*/   
}    
}    

分享到:
评论

相关推荐

    spring+security+11种过滤器介绍.doc

    以下是文档中提到的11种过滤器之一的详细解释: 1. HttpSessionContextIntegrationFilter:这是Spring Security过滤器链的第一个过滤器。它的主要职责是确保每个线程都有一个SecurityContext实例,这个实例存储了...

    STM32的CAN过滤器详解.pdf

    每组过滤器具有可变宽度,可以是32位或16位,并且根据工作模式和宽度,可以配置为以下四种形式: 1. 1个32位的屏蔽位模式过滤器。 2. 2个32位的列表模式过滤器。 3. 2个16位的屏蔽位模式过滤器。 4. 4个16位的列表...

    web过滤器 c#

    "Web过滤器 c#"的讨论涉及了四种主要类型的过滤器,以及如何利用开源库"WebMIS.GoldFilterOpenSource"来增强你的应用功能。理解并熟练运用这些过滤器,可以显著提升你的Web应用的质量和安全性。

    servlet四个有用的过滤器

    在Java Web开发中,`Servlet`过滤器是一种特殊的功能组件,可以在请求到达目标资源(如Servlet或JSP页面)之前进行预处理,也可以在响应发送到客户端之前进行后处理。它们可以用于实现跨切面的需求,比如统一设置...

    拦截器和过滤器的区别

    在现代软件开发过程中,特别是Web应用程序中,为了实现灵活高效的业务逻辑处理及控制流管理,常常会使用到两种设计模式:拦截器(Interceptor)与过滤器(Filter)。这两种技术虽然在功能上有一定的相似之处,但其...

    java中五种常用的过滤器

    本文将详细介绍标题提及的五种常用过滤器,并提供相关实现代码,帮助理解它们的工作原理和用途。 一、使浏览器不缓存页面的过滤器 在Web开发中,有时我们需要确保每次请求都能获取最新的页面内容,防止浏览器缓存...

    java 过滤器(附代码)

    四、其他有用的过滤器 除了上述三个过滤器外,还有其他许多有用的过滤器,例如: * 压缩过滤器:用于压缩响应体,减少网络传输的数据量。 * 安全过滤器:用于检测和防止恶意攻击。 * 记录过滤器:用于记录请求和...

    过滤器笔记整理

    过滤器(Filter)是一种轻量级的、可扩展的应用程序组件,它可以在客户端请求到达目标资源(如Servlet、JSP等)之前对其进行预处理,或者在请求处理完毕后对响应进行后处理。这种机制为开发者提供了极大的灵活性,...

    GridControl的过滤器的自定义

    例如,在上述示例中,我们需要使用 "*0" 查询出四个数据记录(1011、010、201、301),但是使用默认的过滤器机制无法实现这种查询操作。这时,我们需要自定义过滤器以满足特定的查询需求。 自定义过滤器的实现 ...

    servlet过滤器详解

    **过滤器(Filter)**是一种Web组件,它能够在客户端请求到达目标资源(如Servlet、JSP页面等)之前进行预处理,以及在响应返回客户端之前进行后处理。通过这种方式,过滤器能够有效地拦截和修改请求或响应的信息。 ...

    Filter-四个有用的Java过滤器

    在给定的文件中,提到了四个有用的Java过滤器实例,分别是: 1. **使浏览器不缓存页面的过滤器**: 这个过滤器的目的是防止用户浏览器缓存页面,确保每次请求都能获取服务器最新的内容。它通过设置HTTP响应头来...

    accp java过滤器 PPT

    4. **Filter生命周期**:过滤器的生命周期由Servlet容器管理,包括加载、初始化、执行和销毁四个阶段。 5. **过滤器链**:多个过滤器可以通过`@WebFilter`注解或在`web.xml`中配置,定义它们的执行顺序。过滤器链的...

    拦截过滤器模式

    **拦截过滤器模式(Intercepting Filter Pattern)**是一种软件设计模式,主要用于对应用程序的请求或响应进行预处理或后处理。它允许我们在请求到达目标组件之前或响应离开目标组件之后插入一系列过滤器,这些过滤...

    自清洗过滤器 全自动过滤器选型手册

    - **产品简介**:全自动自清洗过滤器是由北京齐天圣马环保设备有限公司研发的一种新型高效过滤器。它能够自动完成过滤及滤芯清洗排污过程,且在清洗排污过程中不影响系统的正常供水,极大地提高了过滤效率和系统的...

    .NET MVC授权过滤器验证登录

    主要有四种类型的过滤器:授权过滤器、操作结果过滤器、异常过滤器和资源过滤器。授权过滤器是最早执行的,通常用于进行身份验证和授权检查。 要创建自定义的授权过滤器,我们需要继承`System.Web.Mvc....

    servlet过滤器技术实例,

    四、过滤器应用场景 1. **安全控制**:验证用户登录状态,防止未授权访问。 2. **数据处理**:如字符编码转换,确保请求和响应的数据格式一致。 3. **性能监控**:记录请求响应时间,帮助优化性能。 4. **日志记录**...

    Hadoop学习四十二:HBase 过滤器

    HBase提供了多种内置过滤器,如SingleColumnValueFilter、RowFilter、PrefixFilter等,每种过滤器都有其特定的应用场景。 1. SingleColumnValueFilter:这种过滤器用于检查特定列族和列的值是否满足用户定义的比较...

    CAN总线标识符过滤器难点解析

    过滤器的两种模式分别为屏蔽位模式和标识符列表模式。在屏蔽位模式下,可以设置特定的位为“必须匹配”或“不关心”,屏蔽位模式允许灵活地匹配特定报文。例如,通过设置CAN_FxR1为期望收到的ID,CAN_FxR2为屏蔽位,...

    过滤器介绍及代码

    ### 过滤器(Filter)概述与应用 #### 一、Filter简介 过滤器(Filter)作为Servlet技术中的一项重要特性,为Web开发者提供了强大的工具,用于对Web服务器上托管的各种资源进行拦截处理。这些资源包括但不限于JSP...

    javaFilter自定义编码过滤器

    * FilterChain 接口:FilterChain 接口是 Java 中的一种过滤器链接口,用于将多个过滤器连接起来,以便实现链式调用。它提供了一个 doFilter() 方法,用于执行下一个过滤器。 二、自定义编码过滤器 在上面的代码中...

Global site tag (gtag.js) - Google Analytics