适用于 HTTP 和 HTTPS 的 Django CSRF
我有一个在 HTTP 和 HTTPS 后面运行的博客,并且遇到了 csrf 令牌验证问题。
CSRF 令牌在每种形式中都可用,但当我在该网站的 HTTP 版本上并尝试提交评论时,我收到以下错误。
禁忌 (403) CSRF验证失败。请求被中止。
引荐来源检查失败 - http://mysite.com/blog/1/ 与 https://mysite.com/。
通过 HTTPS 查看博客时效果很好。
有人知道如何获得验证以匹配两者吗?
I have a blog that runs behind both HTTP and HTTPS and am running into issues with the csrf token verification.
The CSRF token is available in each form as it should be, but when I'm on the HTTP version of the site and try to commit a comment I get the following error.
Forbidden (403)
CSRF verification failed. Request aborted.
Referer checking failed - http://mysite.com/blog/1/ does not match https://mysite.com/.
It works fine when viewing the blog via HTTPS.
Anyone know how to get the verification to match both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要检查您的设置,看看是否将 CSRF_COOKIE_SECURE 参数设置为 True
这会将 cookie 标记为安全,并阻止浏览器通过非安全连接(即 HTTP)接受它。
要检查这是否是问题的根源,您可以使用任何有效的视图并放入断言 False 来获取调试屏幕并查看 Csrf cookie 是否已发送或 nit。
You might want to check your settings to see if you set the CSRF_COOKIE_SECURE parameter to True
This marks the cookie as secure and prevents the browser from accepting it over a non-secure connection, that is, HTTP.
To check whether thus is the source of the issue this you could use any view that works and drop in an assert False to get a debug screen and see whether the Csrf cookie is sent or nit.
我想通了。 的问题
这是我的 fastcgi 参数fastcgi_param HTTPS on
;设置需要启用 HTTPS 的环境变量。当此变量打开时,Django 会对 csrf 令牌执行一些额外的强制操作。
I figured it out. It was an issue with my fastcgi parameters
fastcgi_param HTTPS on;
setting an environ variable that required HTTPS to be on. Django does some extra enforcement for the csrf tokens when this variable is on.