我有以下代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="_csrf" th:content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
<title>Fileupload Test</title>
</head>
<body>
<p th:text="${msg}"></p>
<form action="#" th:action="@{/fileUpload}" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"/>
<input type="submit"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我收到错误HTTP 403:
在请求参数'_csrf'或标题'X-CSRF-TOKEN'上找到无效的CSRF令牌'null'
如果我使用此行,CSRF正在工作:
<form action="#" th:action="@{/fileUpload} + '?' + ${_csrf.parameterName} + '=' + ${_csrf.token}" method="post" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)
但是如果我使用标题,我怎样才能实现工作CSRF?