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中的一种对象,能拦截 ...
随机推荐
- Redis数据类型及基本命令
一.基础命令 提示:Redis不区分命令大小写 1.获得符合规则的键名列表 keys pattern //pattern支持glob风格通配符格式 2.判断一个键是否存在 exists key ...
- npm学习(一)之安装、更新以及管理npm版本
安装npm 安装前须知: npm是在Node中编写的,因此需要安装Node.js才能使用npm.可以通过Node.js网站安装npm,或者安装节点版本管理器NVM. 如果只是想开始探索npm,使用No ...
- 缓存字节流-写coppy方法
package ba; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io. ...
- 3.css3中多个背景图片的用法
(background-clip裁剪,background-position位置,background-origin定位,background-repeat是否重复) <!DOCTYPE htm ...
- ElasticSearch - 解决ES的深分页问题 (游标 scroll)
https://www.jianshu.com/p/f4d322415d29 1.简介 ES为了避免深分页,不允许使用分页(from&size)查询10000条以后的数据,因此如果要查询第10 ...
- 运用在伪类content上的html特殊字符
原文转载于:https://www.cnblogs.com/wujindong/p/5630656.html 项目中用到的一些特殊字符和图标 html代码 <div class="cr ...
- ELK集群搭建
基于5台虚拟机,搭建ELK集群. 方案: 1. ELK是日志分析平台,而不是一款软件,是一整套解决方案,是三个软件产品的首字母缩写,ELK分别代表: Elasticsearch:负责日志检索和储存 L ...
- SQL SERVER 索引维护
-- 全数据库索引重建 DECLARE @name varchar(100)DECLARE authors_cursor CURSOR FOR Select [name] from sysobject ...
- VB之Collection---Collection集合类
你看到的这个文章来自于http://www.cnblogs.com/ayanmw 由于要对一些数据进行处理,比较麻烦,实现某个算法要处理大量不同的不同类型的数据. 所以考虑到一些因素,又在使用VB6( ...
- docker下安装caffe
1.安装docker 2.下载caffe docker镜像 docker pull bvlc/caffe:gpu 可以去https://hub.docker.com/search/?q=SSD%20c ...