尝试上传时 CSRF 令牌 Spring 4 MVC 无效
Posted
技术标签:
【中文标题】尝试上传时 CSRF 令牌 Spring 4 MVC 无效【英文标题】:Invalid CSRF Token Spring 4 MVC while trying to upload 【发布时间】:2015-09-26 20:59:41 【问题描述】:我正在尝试使用 Spring MVC 4 通过 Web 应用程序上传文件,但出现错误:
在请求参数“_csrf”或标头“X-CSRF-TOKEN”上发现无效的 CSRF 令牌“null”。
春季版:
春季版本 4.1.7.RELEASE
Spring Security 4.0.1.RELEASE
代码:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>FATCA Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-web-servlet.xml
</param-value>
</context-param>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-web-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:property-placeholder location="classpath:webapp.properties" />
<context:annotation-config />
<context:component-scan base-package="com.opessoftware" />
<mvc:annotation-driven />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Settings.jsp
<form:form method="POST" action="uploadFile.html"
enctype="multipart/form-data" security="none">
<table style="width: 100%" border="1">
<tr>
<td colspan="1" style="text-align: left"><input
type="file" name="file" style="width: 100%;" /></td>
<td colspan="1" style="text-align: left"><input
type="submit" value=<spring:message code="uploadSettings" /> />
</td>
</tr>
</table>
</form:form>
在提交包含字符串“Test”的名为test.txt
的文件后发布请求负载内容:
Content-Type: multipart/form-data; boundary=---------------------------83935555814334632461054528816
Content-Length: 368
-----------------------------83935555814334632461054528816
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
Test
-----------------------------83935555814334632461054528816
Content-Disposition: form-data; name="_csrf"
41f3dc0a-b97f-4dac-bc49-21e02be53818
-----------------------------83935555814334632461054528816--
【问题讨论】:
CSRF 保护在 Spring Security 4.x 中默认开启。由于您尚未发布 Spring Security 配置,我将假设您尚未将其关闭(否则您不会收到上述错误)。棘手的是,在多部分请求中,每个部分都被单独考虑,因此必须包含 CSRF 令牌。这可能很难实现,因此有一些变通方法。请参阅 this post 了解潜在的解决方法。 【参考方案1】:我按照the accepted answer 到Spring Security 3.2, CSRF and multipart requests 的第二点中的一般建议解决了这个问题,但使用的是 Spring Security 4.0.1.RELEASE 而不是版本 3.2。
更改 1
将 Web 应用程序从 Servlet API 2.4 更新为 Servlet API 3.0:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
....
</web-app>
变化2
使用基于 Java 的配置,创建了一个 StandardServletMultipartResolver
,它使用 Servlet 3.0 的多部分处理并且不需要 commons-fileupload
。请注意,原始实现忽略了显式创建任何多部分解析器,尽管它确实在 Maven 项目中包含了依赖项 commons-fileupload
。
@Configuration
public class WebApplicationConfiguration
@Bean
public MultipartResolver multipartResolver()
return new StandardServletMultipartResolver();
变化3
在DispatcherServlet
中添加了“multipart-config”:
在web.xml
:
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config>
<location>/tmp</location>
<max-file-size>1000000</max-file-size>
<max-request-size>1000000</max-request-size>
<file-size-threshold>10000</file-size-threshold>
</multipart-config>
</servlet>
使用 Java:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer
public SecurityWebApplicationInitializer()
super(SecurityConfiguration.class);
@Override
protected void beforeSpringSecurityFilterChain(ServletContext servletContext)
final XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/spring-web-servlet.xml");
final ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher",
new DispatcherServlet(appContext));
registration.setLoadOnStartup(1);
registration.addMapping("/");
registration.setMultipartConfig(new MultipartConfigElement("", 1000000, 1000000, 100000));
改变 4
从 Maven 项目中删除了依赖项 commons-fileupload
。
【讨论】:
以上是关于尝试上传时 CSRF 令牌 Spring 4 MVC 无效的主要内容,如果未能解决你的问题,请参考以下文章
Struts2 中的文件上传以及 Spring CSRF 令牌
Spring MVC 4 AngularJS 1.3 HTTP POST 具有空 CSRF 令牌或标头