前言
最近在分析源碼時發(fā)現(xiàn)了一個配置如下:
#web.xml文件中cacheSessionFilter org.springframework.web.filter.DelegatingFilterProxy targetFilterLifecycle true #applicationContext.xml文件 /*
那么這個DelegatingFilterProxy到底是干嘛用的那???網(wǎng)上查了很多,最終總結(jié)為一句話即:DelegateFilterProxy實現(xiàn)了服務器容器(tomcate was等)中的filter調(diào)用spring容器中類的功能,那這是如何實現(xiàn)的那?
DelegatingFilterProxy和spring容器如何關(guān)聯(lián)的?觀察web.xml文件,spring的初始化一般都是在非常靠前的,也就是說在使用delegatingfilterproxy之前,spring容器已經(jīng)初始化完成了,然后觀察delegatingfilterproxy的源碼會發(fā)現(xiàn)有這么一段
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); } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/68981.html
摘要:利用這種方式就將或者和業(yè)務對象的依賴關(guān)系用來進行管理,并且不用在中硬編碼要引用的對象名字。配置的的配置完成。推薦使用,應為配置上更簡單。 在使用spring容器的web應用中,業(yè)務對象間的依賴關(guān)系都可以用context.xml文件來配置,并且由spring容器來負責依賴對象 的創(chuàng)建。如果要在filter或者servlet中使用spring容器管理業(yè)務對象,通常需要使用WebApplic...
摘要:構(gòu)建完實例后,將它設置為的父認證管理器并將該傳入構(gòu)造器構(gòu)建實例。,目前為止已經(jīng)被初始化,接下去需要設置對象添加至的列表中打開類結(jié)構(gòu),和一樣,它也實現(xiàn)了接口,同樣繼承自。最后返回的是的默認實現(xiàn)。 最近在整合微服務OAuth 2認證過程中,它是基于Spring Security之上,而本人對Spring Security架構(gòu)原理并不太熟悉,導致很多配置搞不太清楚,遂咬牙啃完了Spring ...
摘要:框架入門簡介是一個能夠為基于的企業(yè)應用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。 1.Spring Security框架入門 1.1 Spring Security簡介 Spring Security是一個能夠為基于Spring的企業(yè)應用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(...
摘要:前言本文主要使用來實現(xiàn)前后端分離的認證登陸和權(quán)限管理,適合和我一樣剛開始接觸前后端完全分離項目的同學,但是你必須自己搭建過前端項目和后端項目,本文主要是介紹他們之間的互通,如果不知道這么搭建前端項目的同學可以先找別的看一下。 前言 本文主要使用spring boot + shiro + vue來實現(xiàn)前后端分離的認證登陸和權(quán)限管理,適合和我一樣剛開始接觸前后端完全分離項目的同學,但是你必...
摘要:客戶端與集成指定端口請求路徑用于單點退出,該過濾器用于實現(xiàn)單點登出功能,可選配置該過濾器用于實現(xiàn)單點登出功能,可選配置。該過濾器使得開發(fā)者可以通過來獲取用戶的登錄名。 CAS客戶端與SpringSecurity集成 pom.xml org.springframework spring-context 4.3.9....
閱讀 2582·2021-09-27 13:36
閱讀 2222·2019-08-29 18:47
閱讀 2202·2019-08-29 15:21
閱讀 1486·2019-08-29 11:14
閱讀 2042·2019-08-28 18:29
閱讀 1691·2019-08-28 18:04
閱讀 630·2019-08-26 13:58
閱讀 3279·2019-08-26 12:12