无模板Django + AJAX:Django的CSRF令牌在浏览会话过程中是否得到更新? [英] Template-less Django + AJAX: Does Django's CSRF token get updated during the course of a browsing session?

查看:261
本文介绍了无模板Django + AJAX:Django的CSRF令牌在浏览会话过程中是否得到更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的设置是AngularJS + Django 1.5,我完全抛弃了使用Django的模板引擎(即后端几乎是一个API服务器)。

My current setup is AngularJS + Django 1.5 and I have completely thrown away the use of Django's template engine (ie. the backend is pretty much an API server).

由于我没有使用 csrf_token 模板标签,反过来,Django不会设置并发送 csrftoken cookie作为回应。根据官方文档的指示,应使用 ensure_csrf_cookie()装饰器强制装饰视图发送 csrftoken 曲奇饼。

Since I am not using the csrf_token template tag, Django, in turn, does not set and send the csrftoken cookie in response. As instructed by the official docs, the ensure_csrf_cookie() decorator should be used to force the decorated view to send the csrftoken cookie.

我已经将 ensure_csrf_cookie()装饰器应用于视图,该视图为我的网络客户端提供了第一个GET请求调用自举。这样,我的网络客户端就会保留CSRF令牌,从而允许对服务器调用不安全的方法(例如POST)。

I have applied the ensure_csrf_cookie() decorator to the view, which serves the first GET request that my web client calls at bootstrapping. With that, my web client gets a hold of the CSRF token and henceforth is allowed to call unsafe methods (ex. POST) to the server.

以上设置工作正常只有在浏览会话结束之前,CSRF令牌保持不变。

The above setup works fine only if the CSRF token remains the same until the browsing session ends.

问题: Django的CSRF令牌是否在浏览会话过程中得到更新?如果是,这是否意味着我需要将 ensure_csrf_cookie()装饰器应用于我拥有的所有视图?

Question: Does Django's CSRF token get updated during the course of a browsing session? If 'yes', does that mean I would need to apply the ensure_csrf_cookie() decorator to all the views I have?

推荐答案


1)Django的CSRF令牌在浏览过程中是否得到更新?

1) Does Django's CSRF token get updated during the course of a browsing session?

看起来像 CSRF 令牌是唯一的每个会话,但它是基于我的意见,我没有官方的来源。使用Angular.js,我使用以下代码没有问题:

Looks like the CSRF token is unique per session, but it is based in my observations, I have no "official" source. With Angular.js I use the following code without problems:

angular.module('app', ...)
  .config(function($httpProvider) {
    var cookies = document.cookie.split(';');
    var csrftoken = _.find(cookies, function(v) { 
                      return v.trim().indexOf('csrftoken=') == 0; 
                    });
    if(csrftoken) {
      $httpProvider.defaults.headers.common['X-CSRFToken'] = csrftoken.split('=')[1];
    }
  })

由于我从Django提供HTML,所以在Angular引导之前,cookie已经存在了。

Since I serve the HTML from Django, by the time Angular bootstraps the cookie is already there.


2)如果是,那是否意味着我需要将ensure_csrf_cookie()装饰器应用于我拥有的所有视图? / p>

2) If 'yes', does that mean I would need to apply the ensure_csrf_cookie() decorator to all the views I have?

您可以尝试 CORS ,而不是 CSRF Otto Yiu 维护 django-cors-headers 包,这个软件包已知与REST框架API正常工作。

You can try CORS instead if CSRF. Otto Yiu maintains the django-cors-headers package, which is known to work correctly with REST framework APIs.

一些(未经测试)的想法应用 ensure_csrf_cookie()

Some (untested) ideas to apply ensure_csrf_cookie():


  • monkey-patch APIView

  • 创建一个CSRFCookie mixin并将其添加到您的视图

  • ensure_csrf_cookie()应用于您的基类

  • monkey-patch APIView
  • create a CSRFCookie mixin and add it to your views
  • apply ensure_csrf_cookie() to your base classes

这篇关于无模板Django + AJAX:Django的CSRF令牌在浏览会话过程中是否得到更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆