`
liufei.fir
  • 浏览: 677063 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

四个有用的Java过滤器

阅读更多
一、使浏览器不缓存页面的过滤器   
import javax.servlet.*;  
import javax.servlet.http.HttpServletResponse;  
import java.io.IOException;   
 
 
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;  
 
 
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());  
     }  
    }  
java源动力,java开源社区,http://www.web-java.com
}  
}  
 
三、字符编码的过滤器  
 
import javax.servlet.*;  
import java.io.IOException;  
 
 
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;   
 
 
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;  
   
 
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");  
}  
   
 
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"); 
分享到:
评论

相关推荐

    Filter-四个有用的Java过滤器

    Filter-四个有用的Java过滤器

    四个有用的java过滤器

    java过滤器 过滤器 J2EE过滤器 filter过滤器

    四个有用的Java过滤器收藏

    四个有用的Java过滤器收藏,实用开发的工具类。

    分别使用管道/过滤器风格、调用/返回风格、回溯法和黑板风格(JavaSpace或自定义黑板结构)4种方法分别实现N皇后问题

    1)可以用C++或者Java撰写,不需要写界面; 2)要求在通用开发平台上可以调试并无误运行; 3)代码长度没有要求; 4)除了所需的头文件、库文件之外,要求代码放在一个文件中; 5)代码需要给出必要的注释,书写风格...

    java面试题20道(适合工作1~3年)(附答案).docx

    Log4j常用的四个日志级别是哪四个?优先级顺序是怎样的?拦截器和过滤器有什么区别?程序中是先执行拦截器还是过滤器?请描述Mybatis结果集中 ResultMap 和ResultType的区别。Java中 @PostConstruct和@PreDestroy...

    Java-web旅游项目实战案例(四个)IDEA项目源码

    Java-web旅游项目实战案例(四个)IDEA项目源码; 4 技术选型 4.1 Web层 a) Servlet:前端控制器 b) html:视图 c) Filter:过滤器 d) BeanUtils:数据封装 e) Jackson:json序列化工具 4.2 Service层 f) Javamail:...

    JAVA上百实例源码以及开源项目

     Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...

    JAVA上百实例源码以及开源项目源代码

     Java局域网通信——飞鸽传书源代码,大家都知道VB版、VC版还有Delphi版的飞鸽传书软件,但是Java版的确实不多,因此这个Java文件传输实例不可错过,Java网络编程技能的提升很有帮助。 Java聊天程序,包括服务端和...

    java开源包11

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包6

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包9

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    21天学通Java Web开发.pdf

    第三篇为Servlet开发篇,介绍了Servlet的基础知识及Servlet监听器,以及过滤器的开发等内容。第四篇为使用设计模式篇,介绍了DAO和MVC这两个经典设计模式。第五篇为框架技术篇,介绍了Struts 2、Hibernate、Spring这...

    java开源包101

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包4

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包5

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包8

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包10

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包3

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java开源包1

    Spring4GWT ...JARP是为petri 网提供的一个Java编辑器,基于ARP分析器。可以将网络图导出为 GIF, JPEG, PNG, PPM, ARP and PNML (XML based)文件格式。使用了优秀的JHotDraw 5.2 框架。 activemq...

    java jdk实列宝典 光盘源代码

    java为数据结构中的映射定义一个接口java.util.Map,有四个实现类HashMap Hashtable LinkedHashMap TreeMap用法和区别;对Map排序; 5字符串 使用String;判断一个字符串是否是合法的java标识符;使用StringBuffer;...

Global site tag (gtag.js) - Google Analytics