【发布时间】:2017-02-08 16:20:25
【问题描述】:
我的项目完全基于基于 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:异常 评估 SpringEL 表达式:“_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 google-app-engine spring-security csrf thymeleaf