- 背景:前端使用thymeleaf,后端使用springboot security,前后端交互post方式一直报错。
- 原因:springboot security 不允许post,具体原因可自查。
- 解决方式:
1.thymeleaf模板中添加:
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
2.js前面添加:
$(function () {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");