2012-04-30 28 views
5

我想了解在Spring-MVC應用程序中定義Spring Security的推薦方式,其中bean定義被分割到多個父/子上下文中。Spring Security + MVC:關於上下文定義和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,它加載我的全球CONFIGS - applicationContext.xmlsecurityContext.xml。 我還定義了彈簧mvc DispatcherServlet/app/,它從spring-mvc-servlet.xml加載它自己的豆。

據我所知,在spring-mvc-servlet.xml中定義的配置對任何一個頂級上下文文件中定義的配置都是不可見的。

那麼定義應用程序級安全性概念的最佳位置在哪裏?例如,我想添加以下過濾器。

<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安全性?

我讀過documentation on Spring-MVC,但關於如何配置它的細節很少。 此外,Spring OAuth examples似乎定義了一個配置文件中的所有內容,這似乎不是真實世界,似乎與我讀過的其他示例相矛盾。

回答

7

第一:內applicationContext.xmlContextLoaderListener)中定義的bean不能訪問spring-mvc-servlet.xmlDispatcherServlet)中定義的一個而不是周圍的其他方式。

你問:


由於彈簧MVC定義了一系列的控制器,這是我要標記爲@Secured的,如何將這些處理如果控制器不是安全可見背景?

所以此工程沒有問題,因爲控制器必須在spring-mvc-servlet.xml來定義的,因此他們「看到」 applicationContext.xml


定義Spring Security的東西,我需要我從移動將servlet.xml添加到全局applicationContext.xml中?

沒有


我需要的彈簧MVC-servlet.xml中內其他配置來告訴它參與到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 or not?它從一個角度另一點討論的主題。

相關問題