我正在使用 Spring Security 和 Thymeleaf。
下面的代码使用的是 JSP:
<c:url var="save" value="/usuario/save?${_csrf.parameterName}=${_csrf.token}"/>
<form:form modelAttribute="usuario" action="${save}" method="post"
enctype="multipart/form-data">
这是我使用 Thymeleaf 的代码,它不起作用:
<form action="#" method="post" th:action="@{/usuario/save?_csrf=(${_csrf.token})}" th:object="${usuario}" enctype="multipart/form-data">
我得到这个网址:
http://localhost:8080/springsecurity/usuario/save?_csrf=&c5cea050-9e39-4220-b7fb-576964def023
但我的值是 _csrf=c5cea050-9e39-4220-b7fb-576964def023
在代码 c5cea050-9e39-4220-b7fb-576964def023< 之前没有
&
/
最佳答案
通常,当您将 Thymeleaf 与 Spring Security 一起使用时,会自动添加一个隐藏的 CSRF 输入。如果不是,请尝试将此添加到您的表单中:
<input type="hidden" name="_csrf" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">
关于java - 如何使用 thymeleaf 做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43837948/