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

2022-08-06 10:27:38

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

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

解决方法:

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

<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_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

  • 作者:cvsea
  • 原文链接:https://blog.csdn.net/JHYPXS/article/details/79429016
    更新时间:2022-08-06 10:27:38