DelegatingFilterProxy类的作用
使用过springSecurity的朋友都知道,首先需要在web.xml进行以下配置
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <init-param>
- <param-name>targetFilterLifecycle</param-name>
- <param-value>true</param-value> <!-- 默认是false -->
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
非springSecurity用法如下:
- <filter>
- <filter-name>DelegatingFilterProxy</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- <init-param>
- <param-name>targetBeanName</param-name>
- <!-- 自定义filter -->
- <param-value>exportExamineFilter</param-value>
- </init-param>
- <init-param>
- <!-- 判断targetFilterLifecycle属性是false还是true,决定是否调用自定义类的init()、destry()方法 -->
- <param-name>targetFilterLifecycle</param-name>
- <param-value>false</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>DelegatingFilterProxy</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
从这个配置中,可能会给我们造成一个错觉,以为DelegatingFilterProxy类就是springSecurity的入口,但其实这个类位于spring-web-3.0.5.RELEASE.jar这个jar下面,说明这个类本身是和springSecurity无关。DelegatingFilterProxy类继承于抽象类GenericFilterBean,间接地implement 了javax.servlet.Filter接口,Servlet容器在启动时,首先会调用Filter的init方法,GenericFilterBean的作用主要是可以把Filter的初始化参数自动地set到继承于GenericFilterBean类的Filter中去。在其init方法的如下代码就是做了这个事:
- PropertyValues pvs = new FilterConfigPropertyValues(filterConfig, this.requiredProperties);
- BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
- ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext());
- bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
- initBeanWrapper(bw);
- bw.setPropertyValues(pvs, true);
另外在init方法中调用了initFilterBean()方法,该方法是GenericFilterBean类是特地留给子类扩展用的
- protected void initFilterBean() throws ServletException {
- // If no target bean name specified, use filter name.
- if (this.targetBeanName == null) {
- this.targetBeanName = getFilterName();
- }
- // Fetch Spring root application context and initialize the delegate early,
- // if possible. If the root application context will be started after this
- // filter proxy, we'll have to resort to lazy initialization.
- synchronized (this.delegateMonitor) {
- WebApplicationContext wac = findWebApplicationContext();
- if (wac != null) {
- this.delegate = initDelegate(wac);
- }
- }
- }
可以看出上述代码首先看Filter是否提供了targetBeanName初始化参数,如果没有提供则直接使用filter的name做为beanName,产生了beanName后,由于我们在web.xml的filter的name是springSecurityFilterChain,从spring的IOC容器中取出bean的代码是initDelegate方法,下面是该方法代码:
- protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
- Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
- if (isTargetFilterLifecycle()) {
- delegate.init(getFilterConfig());
- }
- return delegate;
- }
通过跟踪代码,发现取出的bean是org.springframework.security.FilterChainProxy,该类也是继承于GenericFilterBean,取出bean后,判断targetFilterLifecycle属性是false还是true,决定是否调用该类的init方法。这个FilterChainProxy bean实例最终被保存在DelegatingFilterProxy类的delegate属性里,
下面看一下DelegatingFilterProxy类的doFilter方法
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- // Lazily initialize the delegate if necessary.
- Filter delegateToUse = null;
- synchronized (this.delegateMonitor) {
- if (this.delegate == null) {
- WebApplicationContext wac = findWebApplicationContext();
- if (wac == null) {
- throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
- }
- this.delegate = initDelegate(wac);
- }
- delegateToUse = this.delegate;
- }
- // Let the delegate perform the actual doFilter operation.
- invokeDelegate(delegateToUse, request, response, filterChain);
- }
真正要关注invokeDelegate(delegateToUse, request, response, filterChain);这句代码,在下面可以看出DelegatingFilterProxy类实际是用其delegate属性即org.springframework.security.FilterChainProxy实例的doFilter方法来响应请求。
- protected void invokeDelegate(
- Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
- throws ServletException, IOException {
- delegate.doFilter(request, response, filterChain);
- }
以上就是DelegatingFilterProxy类的一些内部运行机制,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。
此外还要注意一个DelegatingFilterProxy的一个初始化参数:targetFilterLifecycle ,其默认值为false 。 但如果被其代理的filter的init()方法和destry()方法需要被调用时,需要设置targetFilterLifecycle为true。具体可见DelegatingFilterProxy中的如下代码:
- protected void initFilterBean() throws ServletException {
- synchronized (this.delegateMonitor) {
- if (this.delegate == null) {
- // If no target bean name specified, use filter name.
- if (this.targetBeanName == null) {
- this.targetBeanName = getFilterName();
- }
- // Fetch Spring root application context and initialize the delegate early,
- // if possible. If the root application context will be started after this
- // filter proxy, we'll have to resort to lazy initialization.
- WebApplicationContext wac = findWebApplicationContext();
- if (wac != null) {
- this.delegate = initDelegate(wac);
- }
- }
- }
- }
- protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
- Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
- if (isTargetFilterLifecycle()) { //注意这行
- delegate.init(getFilterConfig());
- }
- return delegate;
- }
DelegatingFilterProxy类的作用的更多相关文章
- [转]springSecurity源码分析—DelegatingFilterProxy类的作用
使用过springSecurity的朋友都知道,首先需要在web.xml进行以下配置, <filter> <filter-name>springSecurityFilterC ...
- C++虚基类的作用
虚基类的作用 当一个基类被声明为虚基类后,即使它成为了多继承链路上的公共基类,最后的派生类中也只有它的一个备份.例如:class CBase { }:class CDerive1:virtua ...
- dubbo源码分析4——SPI机制_ExtensionFactory类的作用
ExtensionFactory的源码: @SPI public interface ExtensionFactory { /** * Get extension. * * @param type o ...
- 关于struts2中ActionContext类的作用
关于struts2中ActionContext类的作用有三个: 1.获取三大作用域对象及页面参数 2.是struts标签的上下文对象 3.ThreadLocal内装的就是ActionContext 怎 ...
- JAVA基础加强(张孝祥)_类加载器、分析代理类的作用与原理及AOP概念、分析JVM动态生成的类、实现类似Spring的可配置的AOP框架
1.类加载器 ·简要介绍什么是类加载器,和类加载器的作用 ·Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader ...
- Java中Class和单例类的作用与类成员的理解
Java中Class类的作用与深入理解 在程序运行期间,Java运行时系统始终为所有的对象维护一个被称为运行时的类型标识.这个信息跟踪着每个对象所属的类.JVM利用运行时信息选择相应的方法执行.而保存 ...
- 【转载】C#中SqlCommand类的作用以及常用方法
在C#的数据库操作过程中,SqlCommand类一般用于Sqlserver数据库的SQL语句的执行,包括Select语句.Update语句.Delete语句以及SQL存储过程等,SqlCommand的 ...
- 【转载】C#中SqlConnection类的作用以及常用方法
在C#的数据库编程中,SqlConnection类主要用于连接Sqlserver数据库,使用SqlConnection类的实例方法我们可以打开Sqlserver数据库连接以及获取数据完毕后关闭数据库连 ...
- servlet过滤器Filter使用之DelegatingFilterProxy类
正常情况下,我们需要添加一个过滤器,需要实现javax.servlet.Filter接口,再在web.xml中配置filter,如下: package cc.eabour.webapp.securit ...
随机推荐
- WinForm如何去掉右边和下边的白边
系统给的窗体样式都缺乏美感,想要漂亮的UI只能自己做,很容易实现 1.新建窗体,设置FormBorder为None 这时的窗体就只有一个Panel(Form自带的默认Panel),没有边框,没有标题栏 ...
- EasyUi 合并单元格占列显示
$("#TableContainer").datagrid({ url: '', method: "get&q ...
- Unity/C#基础复习(3) 之 String与StringBuilder的关系
参考资料 [1] @毛星云[<Effective C#>提炼总结] https://zhuanlan.zhihu.com/p/24553860 [2] <C# 捷径教程> [3 ...
- MYsql 之多表查询.
http://www.cnblogs.com/wangfengming/articles/8067220.html
- Day 42 协程. IO 并发
一.什么是协程? 是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 协程相比于线程切换效率更快了. ...
- 769. Max Chunks To Make Sorted
Given an array arr that is a permutation of [0, 1, ..., arr.length - 1], we split the array into som ...
- 字符串(tjoi2016,heoi2016,bzoj4556)(sam(后缀自动机)+线段树合并+倍增+二分答案)
佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为\(n\)的字符串\(s\),和\(m\)个问题.佳媛姐姐必须正确回答这\(m\)个问题, ...
- BZOJ 1001--[BeiJing2006]狼抓兔子(最短路&对偶图)
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 29035 Solved: 7604 Descript ...
- JavaScript基础函数和词法分析以及常用的内置对象和使用方法(4)
day52 参考:https://www.cnblogs.com/liwenzhou/p/8004649.html 函数 函数定义 JavaScript中的函数和Python中的非常类似,只是定义方式 ...
- CISSP一次通过指南(文末附福利)
2017年12月19日,在上海黄浦区汉口路亚洲大厦17层通过了CISSP认证考试,拖拉了一年,终于成绩还算令人满意,为攒人品将自己一年多的复习心得和大家分享,希望能够帮到需要考证的朋友. 本文作者:i ...