简体   繁体   English

授权时CSRF验证失败

[英]CSRF verification failed on authorization

When I'm trying to authorize by 当我尝试通过

curl -X POST -d "grant_type=password&username=myusername&password=mypassword" http://localhost:8000/oauth2/authorize/

I get 403 forbidden: CSRF validation failed. 我得到403禁止:CSRF验证失败。 Request aborted. 请求中止。 I can't understand why, any help would be appreciated. 我不明白为什么,将不胜感激。

By the way I use Django OAuth Toolkit for authentication. 顺便说一下,我使用Django OAuth Toolkit进行身份验证。

Edit: I know about CSRF protection and how to use it in django view and forms, I'm using a package called Django OAuth Toolkit for oauth 2 authorization which shouldn't ask for csrf token. 编辑:我知道CSRF保护以及如何在Django视图和窗体中使用它,我正在使用名为Django OAuth Toolkit的软件包进行oauth 2授权,该软件包不应要求使用csrf令牌。 There is a little information here about what I'm tring to achieve. 有一个小的信息在这里什么我特林实现。

我很to愧地说我将请求发送到错误的URL,我应该将其发送到http://localhost:8000/oauth2/token/

curl -X POST -d "grant_type=password&username=myusername&password=mypassword&client_id=myclientid"  http://localhost:8000/oauth2/token/

You need to add csrf token in your POST data. 您需要在POST数据中添加csrf令牌。

https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

It's probably worth your while reading the documentation on Django's built in Cross Site Request Forgery protection . 阅读Django内置的“ 跨站点请求伪造”保护文档可能很值得。

Any post to a Django view will need the csrfmiddlewaretoken set, otherwise you'll get a 403. You'll need to either include it, or exempt your view from CSRF protection (prefer the former approach if possible). 任何发布到Django视图的帖子都需要csrfmiddlewaretoken集,否则您将获得403。您需要包括它,或者将视图从CSRF保护中豁免 (如果可能,建议使用前一种方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM