如何在子上下文中使用spring安全上下文

如何在子上下文中使用spring安全上下文,spring,spring-security,jboss7.x,spring-ws,Spring,Spring Security,Jboss7.x,Spring Ws,我试图在子上下文中使用spring安全上下文,这样就可以在servlet上下文文件中使用url安全性 我有: <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filt

我试图在子上下文中使用spring安全上下文,这样就可以在servlet上下文文件中使用url安全性

我有:

  <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>
但是,如果我将
部分移到spring安全根上下文配置中,一切都会正常工作。这不应该像我尝试的那样吗?如何在我的子上下文中获得基于url的安全性

我还尝试将上下文文件合并为一个,但似乎出现了相同的问题。

必须转到主应用程序上下文,而不是子(servlet)上下文,因为此安全命名空间元素创建了
springSecurityFilterChain
bean,它由主上下文中的
DelegatingFilterProxy
查找。正如其javadoc明确指出的那样:

xml通常包含一个
DelegatingFilterProxy
定义,指定的
filter name
对应于Spring的根应用程序上下文中的bean名称


默认情况下,DelegatingFilterProxy将在根应用程序上下文中查找,这意味着默认情况下,您需要将
配置放在那里(它创建了springSecurityFilterChain)

但是,您可以通过指定要使用的contextAttribute来指定使用不同的
ApplicationContext
。要执行此操作,请更新web.xml,如下所示

<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>
这是因为它修改了用于查找
ApplicationContext
DelegatingFilterProxy
ServletContext属性的名称。它不再使用默认值来发现根
ApplicationContext
,而是使用
MessageDispatcherServlet
正在使用的属性(从而指向子上下文)

请注意,
MessageDispatcherServlet
(或
FrameworkServlet
的任何子类,如
DispatcherServlet
)使用属性名
org.springframework.web.servlet.FrameworkServlet.CONTEXT>将
ApplicationContext
存储在
ServletContext
中+
其中
是servlet的名称。因此,在本例中,必须配置的属性是
org.springframework.web.servlet.FrameworkServlet.CONTEXT.myapp soap
。如果您将servlet名称
从myapp soap更改为
SpringServlet
,那么您将使用
org.springframework.web.servlet.FrameworkServlet.CONTEXT.SpringServlet


PS我认为主题应该是“如何将spring安全上下文作为子上下文”

您确定spring已经获取了您的spring-security.xml吗?@Maksymdedias是的,因为移动
http
部分会导致在parnt和子上下文中使用所有指令
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
<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>
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";
    }

}