How to protect against CSRF when using Backbone.js to post data?

前端 未结 4 1191
情话喂你
情话喂你 2021-02-04 14:43

Backbone.js handles posting data to server under the hood, so there is no easy way to insert a CSRF token in the payload. How can I protect my site against CSRF in this situatio

4条回答
  •  天命终不由人
    2021-02-04 14:54

    You can use a prefilter to add the token to all requests:

    $.ajaxPrefilter(function(opts) {
        if (opts.data) {
            opts.data += "&";
        }
        opts.data += "csrfToken=" + token;
    });
    

    You may need to add additional logic if you don't always send the token.

提交回复
热议问题