正常情况下,我们需要添加一个过滤器,需要实现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.xml will usually contain a DelegatingFilterProxy definition, with the specified filter-name corresponding 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.init and Filter.destroy lifecycle methods on the target bean, letting the servlet container manage the filter lifecycle.

servlet过滤器Filter使用之DelegatingFilterProxy类的更多相关文章

  1. Servlet过滤器Filter用法

    1 Servlet 过滤器方法 过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法:public void ...

  2. Servlet基础知识(四)——Servlet过滤器Filter

    一.什么是过滤器: 政府大楼的安检保安,它既能对进入政府大楼的人员进行检查,只允许检查符合要求的进入:同时他也负责对出大楼的人进行检查,看他带出的东西是否符合要求. 同样的,Servlet中的过滤器既 ...

  3. Servlet 过滤器Filter

    特点 1)Filter是依赖于Servlet容器,属于Servlet规范的一部分,在Servlet API中定义了三个接口类:Filter, FilterChain, FilterConfig. 2) ...

  4. Servlet 过滤器 Filter

    过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类.javax.servlet.Filter 接口定义了三个方法: 下面是对所有编码过滤器 package filter ...

  5. Servlet过滤器Filter和监听器

    一.Servlet过滤器的概念: *********************************************************************************** ...

  6. servlet过滤器Filter(理论篇)

    为了减少servlet容器在服务器端对信息的判断量,产生了servlet过滤器. servlet过滤器是在java servlet规范2.3中定义的,他能够对servlet容器的请求和响应对象进行检查 ...

  7. JavaWeb学习篇之----Servlet过滤器Filter和监听器

    首先来看一下Servlet的过滤器内容: 一.Servlet过滤器的概念: ************************************************************** ...

  8. Servlet过滤器----Filter

    JavaEE的Servlet规范描述了三种技术:Servlet,Filter,Listener (一)过滤器简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过F ...

  9. SpringBoot 03: 常用web组件 - - - 拦截器 + Servlet + 过滤器

    常用web组件 拦截器 Servlet 过滤器 使用思想 创建自定义类 实现或者继承框架里的接口或类 将自定义类注册到框架中 使用自定义类 拦截器 说明 拦截器是SpringMVC中的一种对象,能拦截 ...

随机推荐

  1. div和span互换

    div是块级元素,它不论大小默认占一行,而且可以设置宽高以及外边距span是行内元素,它占它自身大小的位置,而且不能设置宽高以及边距同时div也可以变为span (display:inline),这样 ...

  2. Apache 的 http-default.conf 详解

    ##Apache 默认设置文件 Timeout 300       #设置服务器在断定请求失败前等待的秒数.默认值 300 KeepAlive Off     #设置是否启用 HTTP 持久链接,On ...

  3. i3wm 配置刷新生效 和 使用mod快捷打开 ranger 小贴士

    在某处学习到了如何配置i3wm后,对其极感兴趣. 学习到的经验总结: Linux中的各种命令操作其实都要首先查阅 man command  或者  command -h  或者  command -- ...

  4. web框架-(三)Django进阶

    通过上节课的学习,我们已经对Django有了简单的了解,现在来深入了解下~ 1. 路由系统 1.1 单一路由对应 url(r'^index$', views.index), 1.2 基于正则的路由 u ...

  5. STL容器Map

    Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)

  6. 安装php-solr扩展

    本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群:   281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...

  7. HTML/CSS实现文字环绕图片布局

    原文: https://blog.csdn.net/yiyelanxin/article/details/75006925 在一个图文并茂的网页上,文字环绕图片可以使布局美观紧凑,如何实现呢?有两种办 ...

  8. LeetCode 1143 最长公共子序列

    链接:https://leetcode-cn.com/problems/longest-common-subsequence 给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序 ...

  9. jquery-时间轴滑动

    效果预览图: html: <div class="tim"> <div class="timdiv"> <div class=&q ...

  10. Java——基础简介

    [历史] 第三代语言: (1)C.Pascal.Fortran面向过程的语言: (2)C++面向过程/面向对象: (3)Java跨平台的纯面向对象的语言: (4).NET跨语言的平台.   第四代语言 ...