2

对于我的模型“Presentacion”,我有一个非常简单的资源

class PresentacionResource(ModelResource):
    model = Presentacion
    fields = (some fields)
    ignore_fields = (few to ignore)

我需要为此实现身份验证,所以当我阅读时,我创建了两个包装器

class AuthListOrCreateModelView(ListOrCreateModelView):
    permissions = (IsAuthenticated, )
class AuthInstanceModelView(InstanceModelView):
    permissions = (IsAuthenticated, )

然后在我的 urls.py 中

url(r'^presentaciones/$', AuthListOrCreateModelView.as_view(resource=PresentacionResource), name='presentacion-root'),
url(r'^presentaciones/(?P<id>[0-9]+)$', AuthInstanceModelView.as_view(resource=PresentacionResource), name='presentacion'),

这适用于 GET 'presentaciones/' 请求,但是当我尝试发出 PUT 请求时,我收到 403 FORBIDDEN

对我来说奇怪的是 GET 工作正常:只要我登录,它就会正确响应,但如果我注销它会以 403 FORBIDDEN 响应。

4

3 回答 3

9

如果问题是 X-CSRF 令牌标头,您可以像这样修改 Backbone.sync 以在每个 POST、PUT、DELETE 请求中发送一个令牌。

        /* alias away the sync method */
        Backbone._sync = Backbone.sync;

        /* define a new sync method */
        Backbone.sync = function(method, model, options) {

            /* only need a token for non-get requests */
            if (method == 'create' || method == 'update' || method == 'delete') {
                // CSRF token value is in an embedded meta tag 
                var csrfToken = $("meta[name='csrf_token']").attr('content');

                options.beforeSend = function(xhr){
                    xhr.setRequestHeader('X-CSRFToken', csrfToken);
                };
            }

            /* proxy the call to the old sync method */
            return Backbone._sync(method, model, options);
        };
于 2012-09-09T05:16:46.430 回答
3

如果您使用 Django 的基于会话的身份验证,那么您可能会绊倒 Django 中内置的 CSRF 保护(请参阅 UserLoggedInAuthentication 类 [1])。

如果是这种情况,您需要确保将 CSRF cookie 发送到客户端,然后您可以调整 jQuery 指令 [2] 以发送带有可能更改数据的请求的 X-CSRFToken 标头。

[1] http://django-rest-framework.org/_modules/authentication.html

[2] https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

于 2012-09-08T20:48:55.570 回答
2

我意识到这是一篇较旧的帖子,但我最近正在处理这个问题。扩展@orangewarp的答案并使用 django 文档(https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax),这是一个解决方案:

此解决方案使用 csrftoken cookie。另一种解决方案是在您的 API 中创建一个 csrf 令牌端点并从那里获取 csrf。

  Backbone._sync = Backbone.sync;

  Backbone.sync = function(method, model, options) {
      //from django docs
      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 = jQuery.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;
      }

      /* only need a token for non-get requests */
      if (method == 'create' || method == 'update' || method == 'delete') {
          var csrfToken = getCookie('csrftoken');

          options.beforeSend = function(xhr){
              xhr.setRequestHeader('X-CSRFToken', csrfToken);
          };
      }

      return Backbone._sync(method, model, options);
  };
于 2014-03-30T02:51:59.723 回答