2013-06-04 91 views
7

我想在子上下文中具有彈簧安全上下文,所以我可以在servlet上下文文件上具有url安全性。如何在子上下文中有彈簧安全上下文

我有:在web.xml中,一般安全配置上的彈簧security.xml文件

<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> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     classpath:/spring-security.xml 
    </param-value> 
    </context-param> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
    <servlet-name>myapp-soap</servlet-name> 
    <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>transformWsdlLocations</param-name> 
     <param-value>true</param-value> 
    </init-param> 
    </servlet> 

<!-- Authorization configurations --> 
<security:http auto-config="false" use-expressions="true" 
    create-session="never" 
    authentication-manager-ref="authenticationManager" 
    entry-point-ref="authenticationEntryPoint"> 

    <security:custom-filter 
     position="PRE_AUTH_FILTER" ref="serviceAuthenticationFilter"/> 

    <security:intercept-url 
     pattern="/GetForbiddenUrl" access="hasRole('roleThatDoesntExist')" /> 
    <security:intercept-url pattern="/**" access="permitAll" /> 
</security:http> 
<!-- annotation security --> 
<security:global-method-security pre-post-annotations="enabled"/> 
在MYAPP皁-servlet.xml中

。它不工作,但失敗

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/my-app/v1/soap]] (ServerService Thread Pool -- 192) JBWEB000284: Exception starting filter springSecurityFilterChain: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined 

但是,如果我移動<security:http>部分彈簧安全根上下文的配置,一切正常。它不應該按我嘗試的方式工作嗎?我如何在我的子上下文中獲得基於URL的安全性?

我也嘗試將上下文文件合併爲一個,但同樣的問題似乎發生。

+0

你確定你的spring-security.xml在春天被選中嗎? –

+0

@MaksymDemidas是的,因爲移動'http'部分導致正在使用的所有指令,在parnt和子內容 – eis

回答

17

的的DelegatingFilterProxy將在根ApplicationContext的,這意味着在默認情況下,你需要在那裏把你的<http>配置默認的外觀(這是什麼創造了springSecurityFilterChain)。

但是,您可以通過指定contextAttribute它使用指定使用該DelegatingFilterProxy不同ApplicationContext。要做到這一點更新你的web.xml如使用Spring安全如下所示

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <init-param> 
     <param-name>contextAttribute</param-name> 
     <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.myapp-soap</param-value> 
    </init-param> 
</filter> 

一個類似的例子3.2 +的AbstractSecurityWebApplicationInitializer可以在下面看到:

public class SecurityApplicationInitializer extends 
     AbstractSecurityWebApplicationInitializer { 

    @Override 
    protected String getDispatcherWebApplicationContextSuffix() { 
     // NOTE: if you are using AbstractDispatcherServletInitializer or 
     // AbstractAnnotationConfigDispatcherServletInitializer You probably 
     // want this value to be "dispatcher" 
     return "myapp-soap"; 
    } 

} 

這工作,因爲它修改的名字DelegatingFilterProxy用於查找ApplicationContext的ServletContext屬性。現在不使用發現根目錄ApplicationContext的默認值,而是使用MessageDispatcherServlet正在使用的屬性(因此指向子上下文)。

注意MessageDispatcherServlet的(或如DispatcherServletFrameworkServlet任何亞類)使用所述屬性名稱"org.springframework.web.servlet.FrameworkServlet.CONTEXT." + <servlet-name>其中<servlet-name>是servlet的名稱存儲在ServletContextApplicationContext。所以在這種情況下,必須配置的屬性是org.springframework.web.servlet.FrameworkServlet.CONTEXT.myapp-soap。如果您將servlet名稱from myapp-soap更改爲spring-servlet,那麼您將使用org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring-servlet代替。

PS我認爲這個問題應該是「如何將彈簧安全上下文作爲子上下文」

+0

哇,聽起來正是我需要的。我會試試這個結果。 – eis

+0

工程就像一個魅力。 – eis

2

<security:http>將不得不去代替孩子(小服務程序)上下文中的主要應用方面,因爲此安全命名空間元素創建springSecurityFilterChain豆,這是在主要方面擡頭通過DelegatingFilterProxy。由於它的javadoc明確指出:

web.xml將通常包含DelegatingFilterProxy定義,具有指定filter-name對應於bean名字在Spring的根應用上下文

相關問題