DelegatingFilterProxy是一个标准servlet Filter的代理,代理实现了Filter接口的spring管理的Bean。支持一个在web.xml的init-param定义的"targetBeanName" filter,在spring applicationContext中指定了target bean的名称。

web.xml通常包含一个DelegatingFilterProxy的定义,它在root springContext中指定了filter-name及对应的bean。在springcontext中实现了标准servlet filter接口的bean在调用时都将被代理。

注意:Servlet Filter接口默认定义的lifecycle方法不会被代理到target bean,它依赖spring applicationContext管理target bean的lifecycle。在filter的属性targetFilterLifecycle的init-param中指定为true将强制执行Filter.init()和Filter.destory()方法,让Filter来管理filter的lifecycle。

 从spring 3.1开始,DelegatingFilterProxy更新到可以支持servlet 3.0基于实例的filter注册方法来构建,通常和spring 3.1的 org.springframework.web.WebApplicationInitializer spi一起使用。这个类由spring security中Ben Alex 写的FilterToBeanProxy灵感触发而写的。

具体实现如下:

1.初始化方法

    @Override
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);
}
}
}
}

初始化代理

    /**
* Initialize the Filter delegate, defined as bean the given Spring
* application context.
* <p>The default implementation fetches the bean from the application context
* and calls the standard {@code Filter.init} method on it, passing
* in the FilterConfig of this Filter proxy.
* @param wac the root application context
* @return the initialized delegate Filter
* @throws ServletException if thrown by the Filter
* @see #getTargetBeanName()
* @see #isTargetFilterLifecycle()
* @see #getFilterConfig()
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
Filter delegate = wac.getBean(getTargetBeanName(), Filter.class);
if (isTargetFilterLifecycle()) {
delegate.init(getFilterConfig());
}
return delegate;
}

2. 触发代理方法

    @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException { // Lazily initialize the delegate if necessary.
Filter delegateToUse = this.delegate;
if (delegateToUse == null) {
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: " +
"no ContextLoaderListener or DispatcherServlet registered?");
}
this.delegate = initDelegate(wac);
}
delegateToUse = this.delegate;
}
} // Let the delegate perform the actual doFilter operation.
invokeDelegate(delegateToUse, request, response, filterChain);
}

触发代理

    /**
* Actually invoke the delegate Filter with the given request and response.
* @param delegate the delegate Filter
* @param request the current HTTP request
* @param response the current HTTP response
* @param filterChain the current FilterChain
* @throws ServletException if thrown by the Filter
* @throws IOException if thrown by the Filter
*/
protected void invokeDelegate(
Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain)
throws ServletException, IOException { delegate.doFilter(request, response, filterChain);
}

3.销毁方法

    /**
* Destroy the Filter delegate.
* Default implementation simply calls {@code Filter.destroy} on it.
* @param delegate the Filter delegate (never {@code null})
* @see #isTargetFilterLifecycle()
* @see javax.servlet.Filter#destroy()
*/
protected void destroyDelegate(Filter delegate) {
if (isTargetFilterLifecycle()) {
delegate.destroy();
}
}

spring之DelegatingFilterProxy的更多相关文章

  1. spring DelegatingFilterProxy管理过滤器

    安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的.它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc),所以它没有 ...

  2. spring DelegatingFilterProxy的原理及运用

    DelegatingFilterProxy的原理及使用 DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理serv ...

  3. DelegatingFilterProxy

    安全过滤器链 Spring Security的web架构是完全基于标准的servlet过滤器的. 它没有在内部使用servlet或任何其他基于servlet的框架(比如spring mvc), 所以它 ...

  4. Spring Security(三十七):Part IV. Web Application Security

    Most Spring Security users will be using the framework in applications which make user of HTTP and t ...

  5. Shiro 与spring 整合的及简单使用(转)

    文章完全转载自: http://www.cnblogs.com/learnhow/p/5694876.html  ,大家可以点击访问原文链接,个人仅作学习收藏 ! 本篇内容大多总结自张开涛的<跟 ...

  6. Spring Security 4.2.3 Filters 解析

    一. 熟悉一个模块的最快方法 1. 配置logback文件,打印相应的debug信息 2. 根据相应的信息,打断点查看执行结果 二.spring 使用 DelegatingFilterProxy 管理 ...

  7. filter中的DelegatingFilterProxy使用事例

    最近发现在filter内使用DelegatingFilterProxy过滤内容,那么为什么不用自带的Filter而使用Spring的DelegatingFilterProxy哪?最后才明白是因为fil ...

  8. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  9. 30分钟学会如何使用Shiro

    本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分 ...

随机推荐

  1. 关闭linux终端进程

    [root@linux-node1 ~]# w 22:16:45 up 24 days, 24 min, 2 users, load average: 0.28, 0.17, 0.15 USER TT ...

  2. ztree实现根节点右击事件,显示添加删除

    需求,右击树节点,出现编辑和删除的提示框 1:在setting 配置里面,给callback设置,右击事件onRightClick: 2:写一个函数onRightClick function onRi ...

  3. openssh 升级到7.5p1

    1. 参照: http://www.cnblogs.com/xiegj/p/5669800.html http://blog.csdn.net/u011080082/article/details/5 ...

  4. Android 开发者不得不面对的六个问题

    一份关于移动应用开发的调查报告显示,Androdid开发者对谷歌的移动操作系统平台的兴趣正在下降.尽管依然有79%的开发者表示对Android “非常感兴趣”,但调查报告显示,一些迹象表明在2012到 ...

  5. hadoop-03-安装java

    hadoop-03-安装java 1,su root 2, rpm -qa|grep jdk #查看已经安装的jdk 3,rpm -e --nodeps `rpm -qa|grep jdk ` #删除 ...

  6. Android ADB工具-截图和录制视频(五)

    Android ADB工具-截图和录制视频(五) 标签(空格分隔): Android ADB 7. 截图和录制视 命令 功能 adb shell screencap –p <path/file& ...

  7. Thinking in States

    Thinking in States Niclas Nilsson PEOPLE IN THE REAL WORLD HAVE A WEIRD RELATIONSHIP WITH STATE. Thi ...

  8. 使用bbed恢复表数据

    对于表级别的数据恢复,ORACLE提供了多种恢复方法:flashback query,logmnr等. 本文通过演示样例演示使用bbed的copy命令恢复用户误删除或者损坏的表数据,当然我们也能够使用 ...

  9. layer是什么

    layer是什么 总结 layer就是一个web弹框 简介 layer是一款web弹层组件,致力于服务各个水平段的开发人员. 可以让你想到即可做到的web弹窗 概述 [1]  layer,一个可以让你 ...

  10. 一次误报引发的DNS检测方案的思考:DNS隧道检测平民解决方案

    摘自:http://www.freebuf.com/articles/network/149328.html 通过以上分析得出监控需要关注的几个要素:长域名.频率.txt类型.终端是否对解析ip发起访 ...