四个有用的过滤器 Filter

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

相关推荐

jQuery HTML5文件上传美化插件jQuery.filer

插件描述:jQuery.filer是一款简单的HTML5文件上传组件美化jQuery插件。它能够完成单文件和多文件的上传,支持文件的拖拽,支持不同的文件格式校验,支持缩略图和图标等,是一款非常实用的文件上传插件。演示地址:http://www.jq22.com/jquery-info11363

解决spring boot shiro的logout过滤器任何请求都会调用的问题

最近发现了一个问题,我写了一个shiro logout过滤器,本意是在退出登录时清空一些缓存,如下: public class CustomLogoutFilter extends LogoutFilter{ private static String checkOutFlag = "checkOut"; private Cache<String, Deque<Seri...

genaro26的博客 3446

UDP_千兆光通信(四)Tri Mode Ethernet MAC ip核

其中 Transmit Engine 从 AXI-Stream TX 接口接收数据,并添加前导码 Preamble、帧起始界定符 SFD、帧校验序列 FSC 等,并在必要时(长度小于最小 MAC 帧长度)填充数据,因此在使用 TEMAC 时,MAC 帧的这几个部分就不需要我们自己添加了(不过其他的 MAC 帧部分,可选的帧过滤器 Frame Filter 用于过滤与一组可配置过滤器匹配的帧,一般用于过滤掉目的 MAC 地址与本设备不一致的帧,TEMAC 默认开启帧过滤器。默认值为使用64位宽的计数器。

lzr232的博客 1784

jQuery.filer文件上传插件简单使用

官网文档 这里贴出使用该插件上传多图片的demo,后端使用python flask。 demo 前端 demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery.filer Demo</title&g...

曲草的博客 1791

海思ISP调优实战:从实验室到真实场景的IPC相机图像质量优化指南

本文深入探讨海思ISP调优在IPC相机图像质量优化中的实战应用,从实验室标定到真实场景部署的全链路调优方法。重点解决光照动态范围、色温变化和噪声特性等核心问题,提供AE权重表配置、CCM标定、3DNR时域滤波等关键技术方案,帮助实现从理论到实践的图像质量提升。

s3t4u的博客 753

A100 NVLink集群极致优化指南

《A100 NVLink集群通信优化指南》摘要:针对A100 80G SXM机型NVLink 3.0集群,提供硬件配置检查方法(nvidia-smi topo -m)和强制启用指令。重点介绍NCCL参数调优(算法选择、低延迟协议配置)、GPUDirect RDMA启用步骤,以及单机/多机场景下的通信模式选择。包含PyTorch all_reduce基准测试方法,并给出带宽异常排查方案(dmesg检查、NCCL缓存重置)。通过CUDA Graph通信重叠和混合精度优化,可实现90%+带宽利用率。附猿界算力监控

2401_89353317的博客 593

ofbiz——关于过滤器ControlFilter的配置

在ofbiz组件的web.xml文件中,我们能看到如下配置: ControlFilter ControlFilter org.apache.ofbiz.webapp.control.ControlFilter allowedPaths /error:/control:/se

xiaozaq的博客 888

MicroCap声学仿真介绍(三)-人耳的频率响应

这里需要注意的是,24mm我们会填为”24m”,这里的“m”是毫的意思,24m意思是24e-3,同理7.4mm我们可以填为7.4m,意思是7.4e-3。MicroCap声学库中,已内置“声源”模块和“管”模块,我们需要调用这两个模块,并用连接线将这两个模块连接到一起,完成仿真工作。上图中第一个图是在管的一端输入1Pa(94dBSPL)的信号,管的另一端的不同频率的声压大小的仿真结果。上述的结果跟我们的常识也是符合的。换言之,我们要仿真的目的是在管的一端加1pa的声压时,管的另一端不同频率下的声压分布。

山无忧的博客 1477

四个有用的过滤器(Filter)

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE

牛牛的梯田 488

27930-2023与27930-2015区别

描述了27930-2023与27930-2015的更新点

manjixingkong1234的专栏 3855

【软考高级架构】论文范文12——论生成式AI在软件开发中的应用与挑战

随着生成式AI技术的跨越式发展,以大语言模型为代表的生成式AI正在深刻改变软件开发的传统模式。在代码生成、测试构造、文档编写等环节,生成式AI展现出显著的效率提升潜力,但其应用也面临着代码质量不可控、安全漏洞植入、版权合规等严峻挑战。本文以笔者主导的某大型金融企业智能研发辅助平台建设项目为案例,围绕生成式AI在软件开发中的应用实践与应对挑战的策略展开论述。笔者担任系统架构师,负责平台架构设计、模型选型、质量保障机制及安全防线构建。

qq837993702的博客 279

四个有用的过滤器Filter(转载)

转自:http://hi.baidu.com/samxx8/blog/item/291a788ddb438c10b31bba35.html一、使浏览器不缓存页面的过滤器     import javax.servlet.*;    import javax.servlet.http.HttpServletResponse;    import java.io.IOExcepti

terry_whut的专栏 657

【声纳与人工智能融合——从理论前沿到自主系统实战】第一章 声纳AI的数据基础:仿真、数据集与表征学习

该数据集的特点是包含了多种底质背景(沙地、泥地、碎石区)与噪声条件,适合验证模型的背景抑制能力。Urashima数据集由日本海洋研究开发机构(JAMSTEC)发布,基于自主水下航行器(AUV)采集,包含前视声纳序列数据,主要用于水下导航与避障算法评估,其数据包含了精确的姿态与深度标注,支持三维场景重建算法的训练。北约海洋研究与实验中心(CMRE)发布的CMRE datasets包含侧扫与合成孔径声纳数据,特别适用于跨传感器域自适应研究,数据集中包含了同一区域的多种声纳成像结果,为跨模态学习提供了基准。

u012133341的博客 104

Java四个有用的过滤器Filter

一、使浏览器不缓存页面的过滤器     import javax.servlet.*;    import javax.servlet.http.HttpServletResponse;    import java.io.IOException;          public class ForceNoCacheFilter implements Filter {       ...

weixin_33725239的博客 87

RK3588平台开发系列讲解(以太网篇)MDIO底层驱动

沉淀、分享、成长,让自己和他人都能有所收获!😄。

内核笔记 6887

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

四个有用的Java过滤器-Filter过滤器的使用

一、使浏览器不缓存页面的过滤器     import javax.servlet.*;    import javax.servlet.http.HttpServletResponse;    import java.io.IOException;       /**   * 用于的使 Browser 不缓存页面的过滤器   */   public class ForceNoCacheFilter

277

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

java 过滤器 例子

四个有用的Java过滤器 一、使浏览器不缓存页面的过滤器     public classForceNoCacheFilter implementsFilter {       public voiddoFilter(ServletRequest request, ServletResponse response,             FilterChainfilterCh

linqing0201的博客 717

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Filter过滤器

过滤器(Filter)用于根据预定义的条件对输入数据进行筛选,只保留满足条件的数据项。这个过程类似于在现实生活中使用筛子筛选谷物,去除杂质,留下纯净的部分。在计算机科学中,过滤器通常接收一个数据集作为输入,并返回一个新的数据集,其中包含原始数据集中符合特定标准的元素。是 JavaWeb三大组件(Servlet、Filter、Listener)之一。

2302_77758423的博客 2385

过滤器Filter

1.过滤器是什么? 过滤器是Servlet的高级特性之一,也别把它想得那么高深,只不过是实现Filter接口的Java类。 1.过滤器是什么?

m0_61609890的博客 3207

Filter的过滤器链

Filter的介绍 过滤器链 我们已经了解了过滤器,并且知道可以配多个过滤器,那么多个滤器之间的执行顺序是什么样的呢 我们建立8个过滤器,demo1,demo2,demo3,和demo11在web.xml配置,demo4,demo5,demo6,和demo41用注解配置 demo1代码 package com.lingaolu.filter; import javax.servlet.*; import java.io.IOException; /** * @aut.

林高禄 6873
上一篇: HQL 语言基本用法(转载)
下一篇: 写Java程序最容易犯的21种错误
优家
博客等级 码龄21年 1粉丝 5原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值