servlet过滤器Filter使用之DelegatingFilterProxy类
正常情况下,我们需要添加一个过滤器,需要实现javax.servlet.Filter接口,再在web.xml中配置filter,如下:
package cc.eabour.webapp.security.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class XssFilter implements Filter { private String enable = null; public void init(FilterConfig filterConfig) throws ServletException {
// Auto-generated method stub
enable = filterConfig.getInitParameter("enable");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// Auto-generated method stub
// Do XSS Filter (WrapperRequest)
chain.doFilter(request, response);
} public void destroy() {
// TODO Auto-generated method stub } }
此时,web.xml中增加的配置:
<filter>
<filter-name>xssFilter</filter-name>
<filter-class>cc.eabour.webapp.security.filter.XssFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>xssFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
那么,我们为什么要使用Spring的org.springframework.web.filter.DelegatingFilterProxy类呢?其中,最主要的目的还是我们添加的过滤器,需要使用spring中的某些bean,即委托Spring来管理过滤器的生命周期。当然,使用了这个代理类,需要设置参数targetFilterLifecycle为true才能让spring来管理,否则就是一个正常的filter,其生命周期会被servlet容器管理。配置如下:
package cc.eabour.webapp.security.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import cc.eabour.webapp.service.IResourceService; @Service("securityXssFilter")
public class XssFilter implements Filter { private String enable = null; @Autowired
private IResourceService reosurceService; public void init(FilterConfig filterConfig) throws ServletException {
// Auto-generated method stub
enable = filterConfig.getInitParameter("enable");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// Auto-generated method stub
// Do XSS Filter (WrapperRequest)
reosurceService.work();
chain.doFilter(request, response);
} public void destroy() {
// TODO Auto-generated method stub } }
web.xml配置:
<filter>
<filter-name>securityXssFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!-- 可以添加自定义参数 -->
<init-param>
<param-name>enable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityXssFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Spring在初始化过滤器的时候,会根据过滤器的名称去寻找对应代理的过滤器,也可以通过参数targetBeanName参数来制定对应的过滤器bean名称。如果把初始化参数targetFilterLifecycle修改为false或不添加,则代理的过滤器为普通的,不受Spring管理。
以下为摘自Spring的文档:
Proxy for a standard Servlet Filter, delegating to a Spring-managed bean that implements the Filter interface. Supports a "targetBeanName" filter init-param in
web.xml, specifying the name of the target bean in the Spring application context.
web.xmlwill usually contain aDelegatingFilterProxydefinition, with the specifiedfilter-namecorresponding to a bean name in Spring's root application context. All calls to the filter proxy will then be delegated to that bean in the Spring context, which is required to implement the standard Servlet Filter interface.This approach is particularly useful for Filter implementation with complex setup needs, allowing to apply the full Spring bean definition machinery to Filter instances. Alternatively, consider standard Filter setup in combination with looking up service beans from the Spring root application context.
NOTE: The lifecycle methods defined by the Servlet Filter interface will by default not be delegated to the target bean, relying on the Spring application context to manage the lifecycle of that bean. Specifying the "targetFilterLifecycle" filter init-param as "true" will enforce invocation of the
Filter.initandFilter.destroylifecycle methods on the target bean, letting the servlet container manage the filter lifecycle.
servlet过滤器Filter使用之DelegatingFilterProxy类的更多相关文章
- Servlet过滤器Filter用法
1 Servlet 过滤器方法 过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法:public void ...
- Servlet基础知识(四)——Servlet过滤器Filter
一.什么是过滤器: 政府大楼的安检保安,它既能对进入政府大楼的人员进行检查,只允许检查符合要求的进入:同时他也负责对出大楼的人进行检查,看他带出的东西是否符合要求. 同样的,Servlet中的过滤器既 ...
- Servlet 过滤器Filter
特点 1)Filter是依赖于Servlet容器,属于Servlet规范的一部分,在Servlet API中定义了三个接口类:Filter, FilterChain, FilterConfig. 2) ...
- Servlet 过滤器 Filter
过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法: 下面是对所有编码过滤器 package filter ...
- Servlet过滤器Filter和监听器
一.Servlet过滤器的概念: *********************************************************************************** ...
- servlet过滤器Filter(理论篇)
为了减少servlet容器在服务器端对信息的判断量,产生了servlet过滤器. servlet过滤器是在java servlet规范2.3中定义的,他能够对servlet容器的请求和响应对象进行检查 ...
- JavaWeb学习篇之----Servlet过滤器Filter和监听器
首先来看一下Servlet的过滤器内容: 一.Servlet过滤器的概念: ************************************************************** ...
- Servlet过滤器----Filter
JavaEE的Servlet规范描述了三种技术:Servlet,Filter,Listener (一)过滤器简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过F ...
- SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器
常用web组件 拦截器 Servlet 过滤器 使用思想 创建自定义类 实现或者继承框架里的接口或类 将自定义类注册到框架中 使用自定义类 拦截器 说明 拦截器是SpringMVC中的一种对象,能拦截 ...
随机推荐
- 08-Django加载静态文件
1.css文件以及js文件要放在static目录下,static和templates属于同级目录 2.在Django项目的同名项目文件的setting.py中,最后添加静态文件夹static目录路径 ...
- Linux架构之Nginx之HTTPS
第52章 Nginx之HTTPS 第52章 Nginx之HTTPS 1.HTTPS安全证书基本概述 1.1 模拟服务器篡改内容 1.1.1 配置目标网站nginx 1.1.2 配置网页 1.1.3 访 ...
- windows 10 安装openssh 0x800f0954 的一种解决方法
安装与卸载参考:https://docs.microsoft.com/zh-cn/windows-server/administration/openssh/openssh_install_first ...
- react 详细解析学习笔记
React的介绍: React来自于Facebook公司的开源项目 React 可以开发单页面应用 spa(单页面应用) react 组件化模块化 开发模式 React通过对DOM的模拟 ...
- rpm安装jdk
rpm安装jdk:(https://blog.csdn.net/daerzei/article/details/80136457) 1.卸载系统自带的JDK rpm -qa|grep java # x ...
- logback系列二:logback在项目中的应用
1.输出http日志 2.输出dubbo日志 3.输出interfacer日志 4.输出到access,remote,app等目录中
- TweenMax的GSAP(GreenSock动画平台)GSAP,专业的Web动画库
很好奇红框框里面的内容是什么,于是点了进去,又百度了下这个英文缩写具体指的什么东西. GSAP的全名是GreenSock Animation Platform,是一个从flash时代一直发展到今天的专 ...
- visual studio 2017激活
VS2017专业版和企业版激活密钥 Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB- ...
- extjs计算两个DateField所间隔的月份(天数)
需求:两个DateField控件,分别为开始时间和结束时间.当选择完结束时间后,自动计算这两个时间段所间隔的月或天数. 需要解决的问题: 1.直接使用Ext.getCmp('endDate').get ...
- POJ 3046 Ant Counting ( 多重集组合数 && 经典DP )
题意 : 有 n 种蚂蚁,第 i 种蚂蚁有ai个,一共有 A 个蚂蚁.不同类别的蚂蚁可以相互区分,但同种类别的蚂蚁不能相互区别.从这些蚂蚁中分别取出S,S+1...B个,一共有多少种取法. 分析 : ...