yar*_*rek 0 javascript ajax csrf csrf-protection
我的 Ajax 调用受“Access-Control-Allow-Origin”保护
但是,仍然可以从 Google Chrome 工具进行一些 ajax 调用。
有没有办法阻止来自 Google Chrome 的 ajax 调用?
(设置CSRF保护的简单方法?)
正如本杰明在第一条评论中所说,永远不要相信客户端,你需要在服务器端进行验证。但我仍然可以给你一个解决方案来阻止用户从控制台发送 Ajax 请求。
您只需要将XMLHttpRequest构造函数设为私有,然后您就可以将其设置为null
(function (xhr) {
// Your code here ...
// Here you can send Ajax Requests without any problem
// You just need to call the sendAjaxReq with options
function sendAjaxReq(options) {
window.XMLHttpRequest = xhr;
$.ajax(options);
window.XMLHttpRequest = null;
}
}(window.XMLHttpRequest));
window.XMLHttpRequest = null;
// Now you can't send Ajax requests from here, and not from console as well.
Run Code Online (Sandbox Code Playgroud)