簡體   English   中英

Spring Security + MVC:關於上下文定義和bean范圍的問題

[英]Spring Security + MVC : Questions around context definition & bean scope

我試圖理解在Spring-MVC應用程序中定義Spring Security的推薦方法,其中bean定義分為多個父/子上下文。

例如,我當前應用程序的web.xml如下所示(我明白這是相當標准的)

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext.xml
    /WEB-INF/securityContext.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

所以,我在/定義了一個標准的ContextLoaderListener ,它加載了我的全局配置 - applicationContext.xmlsecurityContext.xml 我還在/app/定義了spring mvc DispatcherServlet ,它從spring-mvc-servlet.xml加載它自己的bean。

據我了解, spring-mvc-servlet.xml中定義的config對於任何一個頂級上下文文件中定義的配置都不可見。

哪里是定義應用級安全概念的最佳位置? 例如,我想添加以下過濾器。

<security:http pattern="/oauth/token" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint">
    <security:custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
</security:http>

這樣對/app/oauth/token請求就會通過此過濾器,並處理基本身份驗證。

因為這與Spring-MVC應用程序的關注直接相關,所以我最初在spring-mvc-context.xml定義它(這就是為什么app被排除在url之外)。

但是,這意味着它不是在定義的安全配置可見securityContext.xml ,所以它的忽略。

所以,我將它移動到securityContext.xml ,但是這樣做,也必須移動所有依賴項。 我很快就將所有內容移到applicationContext.xml ,這使得spring-mvc-context.xml幾乎為空。

這是常見的嗎? 在頂級上下文中定義的內容與在子上下文中定義的內容之間存在什么分歧?

鑒於spring-mvc定義了一系列控制器,我想將其標記為@Secured ,如果控制器對安全上下文不可見,它們將如何處理?

我是否需要將<mvc:annotation-driven />servlet.xml到全局applicationContext.xml 我是否需要在spring-mvc-servlet.xml進行其他配置以告訴它參與Spring安全性?

我已經閱讀了關於Spring-MVC文檔 ,但是關於如何配置它的細節很少。 此外, Spring OAuth示例似乎在單個配置文件中定義了所有內容,這看起來並不真實,並且似乎與我讀過的其他示例相矛盾。

首先: applicationContext.xmlContextLoaderListener )中定義的bean無法訪問spring-mvc-servlet.xmlDispatcherServlet )中定義的bean,但不能訪問其他方式。

您詢問:


鑒於spring-mvc定義了一系列控制器,我想將其標記為@Secured,如果控制器對安全上下文不可見,它們將如何處理?

所以這沒有問題,因為必須在spring-mvc-servlet.xml定義控制器,所以他們“看到” applicationContext.xml定義的Spring Security內容。


我是否需要將我的servlet.xml移動到全局applicationContext.xml?

沒有


我是否需要在spring-mvc-servlet.xml中進行其他配置以告訴它參與Spring安全性?

沒有


...使spring-mvc-context.xml幾乎為空。 這是常見的嗎?

spring-mvc-context.xml應該包含與Web Stuff相關的所有內容(secrutiy除外)。 因此,公用部分spring-mvc-context.xml是組分系統掃描@Controller ,一些攔截器( mvc:interceptors ), mvc:resourcesmvc:default-servlet-handlermvc:view-controllerReloadableResourceBundleMessageSourceCookieLocaleResolver.SimpleMappingExceptionResolver ......

順便說一句:如果你使用組件掃描,那么你需要其中兩個,一個在applicationContext.xml上掃描@Service @Repository@Component (但不是@Controller ),另一個在spring-mvc-context.xml中只掃描為@Controller


@See也有這個問題: ContextLoaderListener與否? 它從另一個角度討論了這個主題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM