springsecurity使用异步POSt请求报404错误的解决方法

问题原因一:springsecrurity实现了csrf攻击拦截

https://baike.baidu.com/item/CSRF/2735433?fr=aladdin

解决方法:

在html页面头部添加csrf参数:

"_csrf" th:content="${_csrf.token}"/>
"_csrf_header" th:content="${_csrf.headerName}"/>

在js中设置提交请求的head信息

var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader(header, token);
});

问题原因二,请求到了后台,却返回404。Controller忘记写返回状态码 @Responsebody

你可能感兴趣的:(SpringBoot,Spring)