2013-06-04 48 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的根应用上下文

相关问题