日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring源码解析之:Spring Security启动细节和工作模式--转载

發(fā)布時(shí)間:2025/4/5 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring源码解析之:Spring Security启动细节和工作模式--转载 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://blog.csdn.net/bluishglc/article/details/12709557

Spring-Security的啟動(dòng)加載細(xì)節(jié) Spring-Security的啟動(dòng)和Spring框架的啟動(dòng)是一致的,都是從加載并解析xml配置文件開始的,spring通過注冊自己的ServletContextListener:ContextLoaderListener,來監(jiān)聽ServletContext,一旦ServletContext建立完成,spring就開始加載并解析配置文件,然后初始化ioc容器了,具體的方法調(diào)用為: org.springframework.web.context.ContextLoaderListener#contextInitialized

->org.springframework.web.context.ContextLoader#initWebApplicationContext

->org.springframework.web.context.ContextLoader#configureAndRefreshWebApplicationContext ->org.springframework.context.support.AbstractApplicationContext#refresh 到了refresh方法之后,開始進(jìn)行一系列實(shí)質(zhì)性的動(dòng)作了,本文關(guān)心的兩個(gè)重要的動(dòng)作見下圖注釋。這里有一點(diǎn)需要明確的是spring的bean解析和創(chuàng)建bean是兩個(gè)獨(dú)立的過程,在解析時(shí)生成的一種叫beandefinition的對(duì)象(存放于beanFactory的beanDefinitionMap里)代表一個(gè)將要?jiǎng)?chuàng)建的bean實(shí)例的諸多信息(如bean的class類名,構(gòu)造參數(shù),是singleton還是prototype等等)用于指導(dǎo)bean的創(chuàng)建。創(chuàng)建出來的bean實(shí)例存放于beanFactory的xxxxBeanMap、xxxxSingletonObjects等集合字段中。

?

?

每一個(gè)過程: ?加載spring security的配置文件 通過下述方法調(diào)用加載spring security的xml配置文件

org.springframework.web.context.ContextLoaderListener#contextInitialized ->org.springframework.web.context.ContextLoader#initWebApplicationContext ->org.springframework.web.context.ContextLoader#configureAndRefreshWebApplicationContext ->org.springframework.context.support.AbstractApplicationContext#refresh ->org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory ->org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory ->org.springframework.web.context.support.XmlWebApplicationContext#loadBeanDefinitions ->org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions // 自此處開始讀取spring的配置文件并解析之 ->org.springframework.beans.factory.xml.XmlBeanDefinitionReader#doLoadBeanDefinitions ->org.springframework.beans.factory.xml.XmlBeanDefinitionReader#registerBeanDefinitions ->org.springframework.beans.factory.xml.XmlBeanDefinitionReader#createReaderContext ->org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions ->org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#parseBeanDefinitions ->org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parseCustomElement ->org.springframework.security.config.SecurityNamespaceHandler#parse

在org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parseCustomElement方法中,由于namespaceUri是http://www.springframework.org/schema/security,所以使用對(duì)應(yīng)的Handler: org.springframework.security.config.SecurityNamespaceHandler來解析配置文件。我們可以從這個(gè)Handler的Parser列表中看來spring security下的所有一級(jí)元素對(duì)應(yīng)的parser.



接下來,handler.parse()方法會(huì)根據(jù)當(dāng)前的element,找到對(duì)應(yīng)的parser進(jìn)行解析。在我們的示例中,當(dāng)前元素是<http/>,所以會(huì)使用org.springframework.security.config.http.HttpSecurityBeanDefinitionParser來解析<http/>元素。 org.springframework.security.config.http.HttpSecurityBeanDefinitionParser#parse ->org.springframework.security.config.http.HttpSecurityBeanDefinitionParser#createFilterChain ->org.springframework.security.config.http.HttpSecurityBeanDefinitionParser#createSecurityFilterChainBean 方法org.springframework.security.config.http.HttpSecurityBeanDefinitionParser#createFilterChain的一個(gè)重要的動(dòng)作,那就是根據(jù)用戶的配置信息(這些信息已經(jīng)包含在了各種builder中了,也就是代碼中的HttpConfigurationBuilder httpBldr,AuthenticationConfigBuilder authBldr 等)創(chuàng)建相關(guān)的Filter以及FilterChain(一個(gè)org.springframework.security.web.DefaultSecurityFilterChain)自身。不過這個(gè)方法創(chuàng)建的Filter和FilterChain都不是對(duì)應(yīng)Class的真實(shí)實(shí)例,而只是一些place? holer(org.springframework.beans.factory.config.RuntimeBeanReference),到這個(gè)方法結(jié)束時(shí)它們的實(shí)例還沒有初始化.
第二個(gè)過程:實(shí)例化bean 當(dāng)所有element對(duì)應(yīng)的parser都完成解析之后,就開始創(chuàng)建bean的實(shí)例了(包括filter這些inner bean),這個(gè)過程發(fā)生成在方法org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors中,至于具體的初始化過程將在專門的一篇文章里描述,本文不再深究。 Spring-Security的切入點(diǎn) spring security的整個(gè)工作模式是通過Servlet中的Filter機(jī)制,創(chuàng)建一個(gè)由多種Filter和Interceptor組成的FilterChain來實(shí)現(xiàn)的,以下是標(biāo)準(zhǔn)的spring-security嵌入web應(yīng)用的配置方式: [html]?view plaincopy
  • <filter>??
  • ????<filter-name>springSecurityFilter</filter-name>??
  • ????<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>??
  • ????<init-param>??
  • ????????<param-name>targetBeanName</param-name>??
  • ????????<param-value>springSecurityFilterChain</param-value>??
  • ????</init-param>??
  • </filter>??
  • <filter-mapping>??
  • ????<filter-name>springSecurityFilter</filter-name>??
  • ????<url-pattern>/*</url-pattern>??
  • </filter-mapping>??
  • 這里配置了一個(gè)servlet的filter,這個(gè)filter本身并不處理具體的請求,它其實(shí)是一個(gè)filter chain,它內(nèi)部包含了一個(gè)由多個(gè)spring security提供的filter的list,它負(fù)責(zé)把請求委派給list中的每一個(gè)filter進(jìn)行處理。 這個(gè)springSecurityFilterChain的類型是:DefaultSecurityFilterChain,它和它包含的大部分filter都是spring security包提供的類,如前文所述,這些filter實(shí)例都是spring的inner bean,是由spring隱式地初始化并置于容器中管理的。以下就是某種配置下spring建立起來的filter列表:


    這里撿兩個(gè)重要的filter說一下: ? UsernamePasswordAuthenticationFilter:該filter用于用戶初次登錄時(shí)驗(yàn)證用戶身份(authentication)。該filter只在初次認(rèn)證時(shí)存在,一旦認(rèn)證通過將會(huì)從 filter chain中移除。 ? FilterSecurityInterceptor:當(dāng)用戶登入成功之后,每次發(fā)送請求都會(huì)使用該filter檢查用戶是否已經(jīng)通過了認(rèn)證。如果通過了認(rèn)證,就放行,否則轉(zhuǎn)向登錄頁面。 兩個(gè)filter的差別在于: 第一個(gè)負(fù)責(zé)初次登入時(shí)的用戶檢查,這個(gè)檢查需要根據(jù)用戶提供的用戶名和密碼去數(shù)據(jù)庫核對(duì),若存在,將相關(guān)信息封裝在一個(gè)Authentication對(duì)象中。這個(gè)filter可以說是處理初次登錄時(shí)的authentication工作。而第二個(gè)filter則不需要像每個(gè)filter每次都去查詢數(shù)據(jù)庫,它只需要從 security context中查看當(dāng)前請求用戶對(duì)應(yīng)的Authentication?對(duì)象是否已經(jīng)存在就可以了,這個(gè)filter處理的是登入成功之后的authentication工作。這個(gè)filter是需要攔截每次請求的。

    轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/4527024.html

    總結(jié)

    以上是生活随笔為你收集整理的Spring源码解析之:Spring Security启动细节和工作模式--转载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。