【问题标题】:Forbidden (403) CSRF verification failed. Request aborted. Reason given for failure: Origin checking failed does not match any trusted origins禁止 (403) CSRF 验证失败。请求中止。失败的原因:来源检查失败与任何受信任的来源都不匹配
【发布时间】:2021-12-09 07:01:39
【问题描述】:

帮助

失败原因:

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 设置自定义此页面。

【问题讨论】:

  • 我的回答是否解决了您的问题 Erico?

标签: python python-3.x django csrf django-csrf


【解决方案1】:

检查您是否使用 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 中更改:

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

【讨论】:

  • 先生,我正在尝试从 localhost:300 (reactjs) 向 django 发出请求,并且我已经添加了 CSRF_TRUSTED_ORIGINS = ['localhost:3000/'] ,但它仍然说禁止(来源检查失败 - @987654323 @ 不匹配任何受信任的来源。): /api/login/ ,你能帮我解决这个问题吗
【解决方案2】:

2022 年 3 月更新:

如果您的 django 版本"4.x.x"

python -m django --version

// 4.x.x

那么,如果报错如下图:

来源检查失败 - https://example.com 没有 匹配任何受信任的来源。

将此代码添加到“settings.py”

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

在你的情况下,你得到了这个错误:

来源检查失败 - https://praktikum6.jhoncena.repl.co 没有 匹配任何受信任的来源。

因此,您需要将此代码添加到您的“settings.py”

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

【讨论】:

    【解决方案3】:

    事实上,settings.py 中的 CSRF_TRUSTED_ORIGINS 自 Django 4.0 以来似乎是强制性的。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案4】:

    如果像我一样,当源和主机是同一个域时,您会收到此错误。

    可能是因为:

    1. 您正在通过 HTTPS 为您的 django 应用程序提供服务,
    2. 您的 django 应用程序位于代理之后,例如Nginx,
    3. 您忘记在您的settings.py 中设置SECURE_PROXY_SSL_HEADER,例如SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 和/或
    4. 您忘记在服务器配置中设置标头,例如proxy_set_header X-Forwarded-Proto https; 用于 Nginx。

    在这种情况下:

    • 由于 1,来自客户端浏览器的原始标头将是 https://www.example.com
    • 由于 2、3 和 4,request.is_secure() 正在返回 False
    • 意思是_origin_verified()返回False,因为line 285 of django.middleware.csrfhttps://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
    

    请确保在更改此设置之前阅读 https://docs.djangoproject.com/en/4.0/ref/settings/#secure-proxy-ssl-header 中的警告!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 2016-08-27
      • 1970-01-01
      • 2022-07-06
      • 2020-05-04
      • 2017-08-06
      • 2018-11-05
      相关资源
      最近更新 更多