spring之DelegatingFilterProxy

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();
        }
    }
时间: 2024-12-17 06:28:31

spring之DelegatingFilterProxy的相关文章

spring DelegatingFilterProxy的原理及运用

DelegatingFilterProxy的原理及使用 DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过Spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现. DelegatingFilterProxy的使用方法, 首先在web.xml中配置: <filter><

实现一个支持正则匹配的Filter以及Spring管理Filter遇到的问题

相信很多人都会对Http的Filter的url-pattern不支持正则而烦恼吧.就比如,在web项目中Struts,当我们想要对所有的或者某一个Action进行过滤,而不过滤其他的Action,尤其是不想过滤静态资源,如果你的Struts配置了Action后缀可能会好一些,很可惜我的项目就没有设置后缀,所有没法使用url-pattern仅支持的几种规则,所以就自己实现了一个抽象类,用来支持可配置需过滤以及不需过滤的url规则,且支持正则,具体源码可见下方. import java.io.IOE

Spring Security 4.2.3 Filters 解析

一. 熟悉一个模块的最快方法 1. 配置logback文件,打印相应的debug信息 2. 根据相应的信息,打断点查看执行结果 二.spring 使用 DelegatingFilterProxy 管理 filter chain allow the IoC container to manage the lifecycle instead of the servlet container org.springframework.web.filter.DelegatingFilterProxy 是

在Spring MVC中使用Apache Shiro安全框架

我们在这里将对一个集成了Spring MVC+Hibernate+Apache Shiro的项目进行了一个简单说明.这个项目将展示如何在Spring MVC 中使用Apache Shiro来构建我们的安全框架. [TOC] 阅读文章前,您需要做以下准备: Maven 3环境Mysql-5.6+JDK1.7+git环境git.oschina.net帐号Apache Tomcat 7+您熟练掌握的编辑工具,推荐使用InterlliJ IDEA 14+开始项目地址git.oschina.net 项目地

10: shiro总结

原篇地址:http://www.cnblogs.com/learnhow/p/5694876.html 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分对我来说急需在项目中使用的知识加以学习.并且对于大多数第一次接触Shiro的同学来说,掌握这些也应该足够了. 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙

30分钟学会如何使用Shiro

本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分对我来说急需在项目中使用的知识加以学习.并且对于大多数第一次接触Shiro的同学来说,掌握这些也应该足够了. 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙.Shiro的应用不依赖任何容器,它也可以在JavaSE下使用.但是最常用的环境还是JavaEE.下面以

java 项目--安全框架--Shiro

本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分对我来说急需在项目中使用的知识加以学习.并且对于大多数第一次接触Shiro的同学来说,掌握这些也应该足够了. 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙.Shiro的应用不依赖任何容器,它也可以在JavaSE下使用.但是最常用的环境还是JavaEE.下面以

Apache Shiro学习----配置(与SpringMVC集成)

1.web.xml文件中配置 <!--将shiro的配置文件交给Spring监听器初始化--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-shiro-web.xml</param-value> </context-param> <!-

如何使用Shiro

本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分对我来说急需在项目中使用的知识加以学习.并且对于大多数第一次接触Shiro的同学来说,掌握这些也应该足够了. 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙.Shiro的应用不依赖任何容器,它也可以在JavaSE下使用.但是最常用的环境还是JavaEE.下面以