2017-05-07 42 views
0

我正在使用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

enter image description here

+0

它可以與而不是表單一起使用嗎? – Raphael

+0

@拉斐爾我不明白,什麼意思,當我嘗試時,它不提交表格! –

+0

Raphael

回答

0

通常,當你使用Spring Security一起使用Thymeleaf,隱藏CSRF輸入會自動添加。如果不是,請嘗試在您的表單中添加以下內容:

<input type="hidden" name="_csrf" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"> 
+0

謝謝,但我已經嘗試使用這段代碼,但它不起作用,於是我用th:action =」@ {/ usuario/save?(_ csrf = $ {_ csrf.token})}「 –

+0

@PenaPintada爲什麼關閉? – holmis83

+0

沒問題,忘記我要求的。但是,哦,這個:http://stackoverflow.com/questions/43635327/how-to-do-this-using-thymleaf –