django 禁止的(403)CSRF验证失败,请求已中止,失败原因:来源检查失败,与任何可信来源都不匹配

6ljaweal  于 2023-02-10  发布在  Go
关注(0)|答案(4)|浏览(461)

救命
给出的失败原因:

Origin checking failed - https://praktikum6.jhoncena.repl.co does not match any trusted origins.

一般来说,当存在真正的跨站请求伪造,或者Django的CSRF机制没有被正确使用时,就会发生这种情况。对于POST表单,你需要确保:

Your browser is accepting cookies.
The view function passes a request to the template’s render method.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
The form has a valid CSRF token. After logging in in another browser tab or hitting the back button after a login, you may need to reload the page with the form, because the token is rotated after a login.

您看到本页的帮助部分是因为您在Django设置文件中设置了DEBUG = True,将其更改为False,将只显示初始错误消息。
可以使用CSRF_FAILURE_VIEW设置自定义此页。

mmvthczy

mmvthczy1#

检查你是否在使用Django 4.0。我使用的是3.2,升级到4.0时有这个中断。
如果你在4.0,这是我的修正。添加此行到您的settings.py。这是不需要的,当我使用3.2,现在我不能张贴一个表单包含一个CSRF没有它。
CSRF_TRUSTED_ORIGINS = ['https://*.mydomain.com','https://*.127.0.0.1']
查看此行是否需要任何更改,例如,如果需要将https替换为http
根本原因是在4.0中添加了原始标题检查。
https://docs.djangoproject.com/en/4.0/ref/settings/#csrf-trusted-origins
Django 4.0中的更改:

在旧版本中不执行原始标头检查。

uubf1zoe

uubf1zoe2#

2022年3月更新:

如果您的django版本是**"4.x.x"**:

python -m django --version

// 4.x.x

然后,如果错误如下所示:
来源检查失败-https://example.com与任何可信来源都不匹配。
将此代码添加到**"www.example.com":settings.py":

CSRF_TRUSTED_ORIGINS = ['https://example.com']

在您的示例中,您得到了以下错误:
来源检查失败-https://praktikum6.jhoncena.repl.co与任何可信来源都不匹配。
因此,您需要将此代码添加到您的"www.example.com":settings.py"**:

CSRF_TRUSTED_ORIGINS = ['https://praktikum6.jhoncena.repl.co']
bis0qfac

bis0qfac3#

源和主机是同一个域

如果像我一样,当源和主机是同一个域时,您会得到此错误。
这可能是因为:
1.您通过HTTPS提供django应用程序,
1.您的django应用位于代理(例如Nginx)之后,
1.您忘记在settings.py中设置SECURE_PROXY_SSL_HEADER,例如SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')和/或
1.您忘记在服务器配置中设置头文件,例如Nginx为proxy_set_header X-Forwarded-Proto https;
在这种情况下:

  • 由于1,来自客户端浏览器的原始标头将为https://www.example.com
  • 由于2、3和4,request.is_secure()返回False
  • 这意味着_origin_verified()返回False是因为django.middleware.csrf的第285行(https://www.example.comhttp://www.example.com的比较):
def _origin_verified(self, request):
        request_origin = request.META["HTTP_ORIGIN"]
        try:
            good_host = request.get_host()
        except DisallowedHost:
            pass
        else:
            good_origin = "%s://%s" % (
                "https" if request.is_secure() else "http",
                good_host,
            )
            if request_origin == good_origin:
                return True
    • 在更改此设置之前,请务必阅读www.example.com中的警告!https://docs.djangoproject.com/en/4.0/ref/settings/#secure-proxy-ssl-header before changing this setting though!
6mw9ycah

6mw9ycah4#

您也可能因为在Proxmox上使用容器而出现此错误。
如果您的https域名是由Proxmox通过内部http连接路由的,则会出现此错误。
域名(https)=〉Proxmox =〉(http)=〉包含Django的容器:CSRF错误
我遇到了这个错误,并通过https内部连接更改了通过Proxmox到我的容器的路由(我必须在我的CT上创建并签署证书)。
域名(htps)=〉Proxmox =〉(https)=〉Django所在的容器
自从Django的CSRF错误消失后。

相关问题