资讯专栏INFORMATION COLUMN

DelegatingFilterProxy分析

vboy1010 / 1944人阅读

前言

最近在分析源码时发现了一个配置如下:

#web.xml文件中

    cacheSessionFilter
    org.springframework.web.filter.DelegatingFilterProxy
    
        targetFilterLifecycle
        true
    


    
    /*


#applicationContext.xml文件

那么这个DelegatingFilterProxy到底是干嘛用的那???网上查了很多,最终总结为一句话即:DelegateFilterProxy实现了服务器容器(tomcate was等)中的filter调用spring容器中类的功能,那这是如何实现的那?

DelegatingFilterProxy和spring容器如何关联的?

观察web.xml文件,spring的初始化一般都是在非常靠前的,也就是说在使用delegatingfilterproxy之前,spring容器已经初始化完成了,然后观察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);
                }
            }
        }
    }

没错WebApplicationContext其实就是spring容器,也就是说DelegatingFilterProxy中保存有spring的容器,而在WebApplicationContext中有一个和DelegatingFilerProxy同名的类(这个类就是我们自己的类),DelegatingFilterProxy会从WebApplicationContext中寻找那个和其同名的类,然后将所有的动作赋予给它。

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) {
                delegateToUse = this.delegate;
                if (delegateToUse == null) {
                    WebApplicationContext wac = findWebApplicationContext();
                    if (wac == null) {
                        throw new IllegalStateException("No WebApplicationContext found: " +
                                "no ContextLoaderListener or DispatcherServlet registered?");
                    }
                    delegateToUse = initDelegate(wac);
                }
                this.delegate = delegateToUse;
            }
        }

        // Let the delegate perform the actual doFilter operation.
        invokeDelegate(delegateToUse, request, response, filterChain);
    }

其中的delegateToUse就是从webapplicationcontext中取出来的:

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/68981.html

相关文章

  • 如何使用Spring管理Filter和Servlet

    摘要:利用这种方式就将或者和业务对象的依赖关系用来进行管理,并且不用在中硬编码要引用的对象名字。配置的的配置完成。推荐使用,应为配置上更简单。 在使用spring容器的web应用中,业务对象间的依赖关系都可以用context.xml文件来配置,并且由spring容器来负责依赖对象 的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplic...

    amuqiao 评论0 收藏0
  • Spring Security 初始化流程详解

    摘要:构建完实例后,将它设置为的父认证管理器并将该传入构造器构建实例。,目前为止已经被初始化,接下去需要设置对象添加至的列表中打开类结构,和一样,它也实现了接口,同样继承自。最后返回的是的默认实现。 最近在整合微服务OAuth 2认证过程中,它是基于Spring Security之上,而本人对Spring Security架构原理并不太熟悉,导致很多配置搞不太清楚,遂咬牙啃完了Spring ...

    tommego 评论0 收藏0
  • spring系列---Security 安全框架使用和文件上传FastDFS

    摘要:框架入门简介是一个能够为基于的企业应用系统提供声明式的安全访问控制解决方案的安全框架。 1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(...

    K_B_Z 评论0 收藏0
  • sSpring Boot多模块+ Shiro + Vue:前后端分离登陆整合,权限认证(一)

    摘要:前言本文主要使用来实现前后端分离的认证登陆和权限管理,适合和我一样刚开始接触前后端完全分离项目的同学,但是你必须自己搭建过前端项目和后端项目,本文主要是介绍他们之间的互通,如果不知道这么搭建前端项目的同学可以先找别的看一下。 前言 本文主要使用spring boot + shiro + vue来实现前后端分离的认证登陆和权限管理,适合和我一样刚开始接触前后端完全分离项目的同学,但是你必...

    macg0406 评论0 收藏0
  • spring系列---CAS客户端与SpringSecurity集成

    摘要:客户端与集成指定端口请求路径用于单点退出,该过滤器用于实现单点登出功能,可选配置该过滤器用于实现单点登出功能,可选配置。该过滤器使得开发者可以通过来获取用户的登录名。 CAS客户端与SpringSecurity集成 pom.xml org.springframework spring-context 4.3.9....

    hizengzeng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<