簡體   English   中英

CSRF驗證在django / backbone.js中失敗

[英]CSRF verification failing in django/backbone.js

我正在嘗試從輕量級django重新創建一個小項目-https: //github.com/lightweightdjango/examples/tree/chapter-5

嘗試使用超級用戶帳戶登錄時出現CSRF錯誤。 下面是我的models.js

(function ($, Backbone, _, app) {

    // CSRF helper functions taken directly from Django docs
    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/i.test(method));
    }

    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(
                    cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }

    // Setup jQuery ajax calls to handle CSRF
    $.ajaxPrefilter(function (settings, originalOptions, xhr) {
        var csrftoken;
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            // Send the token to same-origin, relative URLs only.
            // Send the token only if the method warrants CSRF protection
            // Using the CSRFToken value acquired earlier
            csrftoken = getCookie('csrftoken');
            xhr.setRequestHeader('X-CSRFToken', csrftoken);
        }
    });

我嘗試將整個項目克隆到本地文件夾。 我仍然收到CSRF錯誤。

Django僅提供項目的API-模板等由Backbone.js處理

請讓我知道是否需要發布更多代碼。

我的登錄模板(如果有幫助)

  var LoginView = FormView.extend({
        id: 'login',
        templateName: '#login-template',
        submit: function (event) {
            var data = {};
            FormView.prototype.submit.apply(this, arguments);
            data = this.serializeForm(this.form);
            $.post(app.apiLogin, data)
                .done($.proxy(this.loginSuccess, this))
                .fail($.proxy(this.failure, this));
        },
        loginSuccess: function (data) {
            app.session.save(data.token);
            this.done();
        }
    });

從代碼示例中尚不清楚您是否要定義CRSF令牌。 如果您使用的是django模板,則可以在代碼中的某個位置將{% csrf_token %}設置為CRSF令牌。

我有完全一樣的問題。 然后按照該書第111頁的建議:

假設項目使用默認的cookie名稱csrftoken 如果需要,可以通過app.js解析的配置來配置此令牌。

我在index.html的“配置”部分中添加了"csrftoken": "{% csrf_token %}"

...
<script src="{% static 'board/vendor/backbone.js' %}"></script>
<script id="config" type="text/json">
    {
        "models": {},
        "collections": {},
        "views": {},
        "router": null,
        "csrftoken": "{% csrf_token %}", //added this
        "apiRoot": "{% url 'api-root' %}",
        "apiLogin": "{% url 'api-token' %}"
    }
</script>
<script src="{% static 'board/js/app.js' %}"></script>
...

進行此更改后,該錯誤已修復,並且我能夠登錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM