Jsp 上载时HTTP 403错误-无效的CSRF令牌';空';
此文件包含用于上载文件的表单 uploadForm.jspJsp 上载时HTTP 403错误-无效的CSRF令牌';空';,jsp,spring-mvc,spring-security,apache-commons-fileupload,Jsp,Spring Mvc,Spring Security,Apache Commons Fileupload,此文件包含用于上载文件的表单 uploadForm.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %> <html> <head> <m
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<sec:csrfMetaTags/>
<title>File Upload</title>
</head>
<body>
<jsp:include page="/resources/layout/header.jsp"/> <!-- Header -->
<div class="container">
<form action="uploadfile" method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="file"><br />
Name: <input type="text" name="name"><br /> <br />
<input type="submit" value="Upload"> Press here to upload the file!
</form>
</div> <!-- Container -->
<jsp:include page="/resources/layout/footer.jsp"/> <!-- Footer -->
</body>
</html>
我上传时出现以下错误:
HTTP状态403-在请求参数“\u CSRF”或标头“X-CSRF-Token”上发现无效的CSRF令牌“null”
我也使用了spring security。但我总是犯同样的错误。我试了很多,但都解决不了。请帮助解决此问题。您的Spring应用程序中的CSRF(跨站点请求伪造)保护似乎已启用。实际上它是默认启用的
根据:
什么时候应该使用CSRF保护?我们建议使用CSRF
保护浏览器可以通过以下方式处理的任何请求:
普通用户。如果您仅创建由使用的服务
非浏览器客户端,您可能希望禁用CSRF保护
因此,要禁用它:
@Configuration
public class RestSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
如果您希望保持CSRF保护处于启用状态,则必须在表单中包含csrftoken
。您可以这样做:
<form .... >
....other fields here....
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
</form>
……这里的其他字段。。。。
您甚至可以在表单的操作中包含CSRF令牌:
<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data">
感谢您的响应f,但我是通过xml本身进行配置的。在spring security 4上,添加到表单操作URL是可行的,但添加为隐藏字段不起作用。这有点晚了,但以防其他人遇到与digz相同的问题,确保主上下文xml文件中有filterMultipartResolver bean,而不是servlet xml。如果不是jsp,我们如何生成csrf令牌并从ionicSee等移动混合框架发送请求。
<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data">