如何同时为 Spring 安全性正确定义基于 web.xml 和 java 的配置

How to correctly define web.xml and java-based config for Spring Security at the same time

我的项目完全基于 java 的配置。所以我有这个而不是标准 web.xml:

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(WebAppConfiguration.class);
          AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

          ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
    }

效果很好,我能够像这样在 Thymeleaf 模板中获取 csrf 令牌:

<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>

但现在我需要将我的项目部署到 Google App Engine,它需要 web.xml 否则它甚至无法启动。 我添加了 web.xml,并删除了上面的 java 配置:

<context-param>
        <param-name>contextClass</param-name>
        <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.demshin.medpro.configuration.WebAppConfiguration</param-value>
    </context-param>

    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

当我尝试打开我的应用 url 时,出现异常:

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "_csrf.token"

正如我所想,问题出在损坏的过滤器链中。 但是,如果我将其添加到 web.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>

而不是这个 java 配置:

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    public SecurityWebApplicationInitializer() {
        super(WebSecurityConfiguration.class);
    }
}

,虽然轨迹有点不同,但问题仍然存在。

那么如何治愈呢?也许有办法摆脱 Google App Engine 上的 web.xml? 谢谢。

如果您保留 java 配置然后创建一个空的 web.xml 会怎样,例如

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
     version="3.1">

</web-app>

我找到了一些有用的信息。 问题是 WebApplicationInitializer 需要 Servlet 3.0,但 Appengine 只支持 Servlet 2.5。所以我们必须使用普通的基于 XML 的配置,至少在初始化时是这样。并在web.xml中手动配置Springfilter/servlet。 不幸的是,我没有完全将 spring 配置移动到 xml 就无法使其工作,只是添加 spring-security.xml 没有工作。

我们也可以投票支持 Servlet 3.0 here,但不幸的是 Google 似乎没有任何添加它的计划。