【问题标题】:How to allow POST restful webservice calls with Django Piston and CSRF protection?如何使用 Django Piston 和 CSRF 保护允许 POST RESTful Web 服务调用?
【发布时间】:2011-08-21 10:33:47
【问题描述】:

我是 django-piston 的新手,由于 Django 的 CSRF 保护,我无法让 POST 网络服务调用正常工作。如何允许 web 服务调用绕过 CSRF 保护并仍然允许其余网页保留它们?

【问题讨论】:

    标签: django django-piston


    【解决方案1】:

    找到解决方案:https://bitbucket.org/jespern/django-piston/issue/82/post-requests-fail-when-using-django-trunk,感谢 Brian Zambrano。

    我觉得这有点烦人,这是两年前的事,已经创建了一个补丁,但仍然没有合并到最新的源中。

    要修复它,通过添加 self.csrf_exempt = getattr(self.handler, 'csrf_exempt', True) 将补丁应用到您的活塞/resource.py 文件,如下所示:

             self.handler = handler()
    +        self.csrf_exempt = getattr(self.handler, 'csrf_exempt', True)
    
             if not authentication:
                 self.authentication = NoAuthentication()
    

    【讨论】:

      【解决方案2】:

      为什么需要这样做? CSRF 是一个简单的域检查......但如果你真的需要它,答案在文档中:https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#csrf-protection-should-be-disabled-for-just-a-few-views

      【讨论】:

      • CSRF 不像域检查那么简单。
      猜你喜欢
      • 2016-01-12
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2011-08-04
      • 2013-12-29
      • 2011-02-21
      • 2014-09-13
      相关资源
      最近更新 更多