[英]Spring Security 4 JTwig put CSRF token in forms
以下代碼為我工作:
我創建了一個名為ControllerSetup的類(或者您可以隨意命名),並將其放置在與Application類相同的文件夾中(帶有public static void main()
方法的文件夾)。 代碼如下:
package some.pkg.of.myapp;
import javax.servlet.http.HttpServletRequest;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
@ControllerAdvice
public class ControllerSetup {
@ModelAttribute
public void initModel(HttpServletRequest request, Model model) {
model.addAttribute("_csrf", request.getAttribute("_csrf"));
}
}
現在,我的任何控制器中的任何模型都將自動具有名為_csrf的屬性。 我將在我的JTwig表單中使用它,如下所示:
<form method="post" action="/some/action/url">
<!-- My fields and buttons here -->
<input type="hidden"
name="{{ _csrf.parameterName }}" value="{{ _csrf.token }}" />
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.