小编use*_*811的帖子

将CSRF放入Spring 4.0.3 + Spring Security 3.2.3 + Thymeleaf 2.1.2中的Headers中

我有以下代码:

<!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?

security spring csrf thymeleaf

16
推荐指数
3
解决办法
9190
查看次数

标签 统计

csrf ×1

security ×1

spring ×1

thymeleaf ×1