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中的一种对象,能拦截 ...
 
随机推荐
- 执行命令npm publish报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest2 - You cannot publish over the previously published versions: 0.0.1.
			
前言 执行命令npm publish报错:403 Forbidden - PUT https://registry.npmjs.org/kunmomotest2 - You cannot publis ...
 - mysql基础知识和pymysql
			
一.视图 视图是指计算机数据库中的视图,是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的 ...
 - CentOS7 安装 Mysql5.6.40
			
CentOS7.5二进制安装MySQL-5.6.40 安装之后登陆不上,mysql.user 表是空的时: Mysql User表为空 mysql创建用户报错ERROR 1364 (HY000): F ...
 - 通过关键字Event定义用户自己的事件
			
Event 语句 定义用户自定义的事件. 语法[Public] Event procedurename [(arglist)] Event 语句包含下面部分: 部分 描述 Public 可选的.指定该 ...
 - Ansbile实战经验
			
一.相关用法: 1.执行shell 获取web组里得eth0接口信息 ansible web -a "ifconfig eth0" 2.执行ifconfig eth0 命令,ans ...
 - Qt的QSettings类和.ini文件读写
			
Detailed Description QSettings类提供了持久的跨平台的应用程序设置.用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表 ...
 - java String源码浅出
			
1.public char charAt(int index) 返回指定索引处的 char 值. 源码: =====================String.class============== ...
 - springboot 集成p6spy
			
pom.xml <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifact ...
 - 28.密码学知识-hash函数-5——2019年12月19日
			
5. 单向散列函数 "单向散列函数 --- 获取消息的指纹" 在刑事侦查中,侦查员会用到指纹.通过将某个特定人物的指纹与犯罪现场遗留的指纹进行对比,就能够知道该人物与案件是否存在关 ...
 - 花式赋值、列表、字典、解压缩、input()、格式化学习笔记
			
目录 花式赋值 列表(list) 字典(dict) 解压缩 input()与用户交互 格式化的三种方式 f_String格式化(important) %s.%d占位符 format 格式化(不常用) ...