我将 Spring Security 与 CSRF 结合使用,我的 javascript 中的一些 POST 调用有问题。对于我的页面,我使用 Thymeleaf 和 HTML 5,对于我的 Controller 的 Rest 调用,我使用 jQuery.ajax。 如果我像这样对我的表单使用 ajax 调用:
$(function() {
$("#saveCarButton").click(
function() {
var form = $("#addCarForm");
$.ajax({
type : "POST",
url : form.attr("action"),
data : form.serialize(),
// all right with rest call
success : function(data) {...}
//error during rest call
error : function(data) {
window.location.href = "/500";
}
});
});
});
一切正常,但是当我调用这个函数时:
jQuery.ajax({
url : 'upload',
type: 'POST',
contentType: false,
processData: false,
data:formData,
beforeSend:function(xhr) {
waitingModal.showPleaseWait();
},
success: function(data,status,xhr){...}
error: function(xhr,status,e){
}
}).complete(function() {
//add timeout because otherwise user could see a too fast waiting modal
setTimeout(function(){
waitingModal.hidePleaseWait();
}, 1000);
});
我收到错误 403。 也许在表单中,使用 thymeleaf,它工作正常,但对于第二种类型的请求,我必须添加 CSRF token 。 我试过
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
在我的 html 页面中添加了
!-- -->
<meta name="_csrf" th:content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
为什么它与表单一起工作? 使用表格时我不必添加任何内容吗? 使用浏览器的开发工具可以看到 _csrf 和 _csrf_header 是否有危险? 谢谢
最佳答案
这个解决方案对我有用 Trying to use React/Ajax calls with Spring MVC and Thymeleaf
<meta id="_csrf" name="_csrf" th:content="${_csrf.token}"/>
<meta id="_csrf_header" name="_csrf_header" th:content="${_csrf.headerName}"/>
Ajax :
var token = $('#_csrf').attr('content');
var header = $('#_csrf_header').attr('content');
$.ajax({
type: "POST",
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader(header, token);
},
success: function(data, textStatus, jqXHR) {
alert(status);
},
error: function(request, status, error) {
alert(status);
}
});
关于jquery - 使用带有 ajax Rest 调用的 Spring CSRF 和带有 Thymeleaf 的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34747437/