Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以将SpringMVCServlet的WebApplicationContext注入DelegatingFilterProxy吗?_Spring_Spring Mvc_Spring Security - Fatal编程技术网

我可以将SpringMVCServlet的WebApplicationContext注入DelegatingFilterProxy吗?

我可以将SpringMVCServlet的WebApplicationContext注入DelegatingFilterProxy吗?,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我正在创建一个OAuth授权服务器,它使用Spring安全性作为servlet部分的安全层。其中一个重要部分是使用DelegatingFilterProxy映射到springSecurityFilterChain bean,它需要一个WebApplicationContext实例 标准解决方案是包括一个带有相关contextConfigLocation配置的ContextLoaderListener。但这需要为rootWebApplicationContext创建一个单独的配置,在我看来这不必要

我正在创建一个OAuth授权服务器,它使用Spring安全性作为servlet部分的安全层。其中一个重要部分是使用
DelegatingFilterProxy
映射到springSecurityFilterChain bean,它需要一个
WebApplicationContext
实例

标准解决方案是包括一个带有相关contextConfigLocation配置的
ContextLoaderListener
。但这需要为root
WebApplicationContext
创建一个单独的配置,在我看来这不必要地使事情复杂化

根据Spring MVC文档,每个
DispatcherServlet
都有自己的
WebApplicationContext
实例。此外,通过阅读
DelegatingFilterProxy
的代码,应该可以在构建时插入
WebApplicationContext
实例

因此,我的问题是:我可以将DispatcherServlet WebApplicationContext设置为DelegatingFilterProxy的实例吗?

以下是我目前的相关配置:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
            http://xmlns.jcp.org/xml/ns/javaee
            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <!-- Enable Spring Security -->
    <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>oauth</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>    
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/servlet.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>oauth</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
非统组织
org.springframework.web.servlet.DispatcherServlet
1.
上下文配置位置
/WEB-INF/servlet.xml
非统组织
/
servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/security/oauth2
        http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <mvc:annotation-driven/>

    <!-- ... Spring MVC config ... -->

    <!-- Spring Security OAuth Config -->
    <security:global-method-security pre-post-annotations="enabled" />

    <oauth:authorization-server client-details-service-ref="clientDetails"
                                token-services-ref="tokenServices"
                                token-endpoint-url="/api/token">
        <oauth:refresh-token/>
        <oauth:client-credentials/>
    </oauth:authorization-server>

    <!-- ... loads more OAuth config ... -->

</beans>

DispatcherServlet
(与
FrameworkServlet
的任何子类一样)将使用属性名
org.springframework.web.servlet.FrameworkServlet.CONTEXT.
ServletContext
中发布其
web应用上下文

同时,
DelegatingFilterProxy
可以通过设置其
contextAttribute
参数被告知不要使用root
WebApplicationContext
,而是使用另一个存储在
ServletContext
中的根

在您的情况下,所需配置为:

<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.oauth </param-value>
    </init-param>
</filter>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
上下文属性
org.springframework.web.servlet.FrameworkServlet.CONTEXT.oauth

查看有关如何在的javadoc中查找
WebApplicationContext
DelegatingFilterProxy
的更多信息。

谢谢!我知道contextAttribute,但我找不到它的文档或它需要的值。