Django Localhost CORS 不工作

Django Localhost CORS not working

我有一个本地 Django 设置如下

Django Rest Framework:localhost:8000

AngularJS frontend:local apache running on http://localservername

我已经安装了 django-cors-headers 并且在我的 settings.py 中,我已经设置了我的

CORS_ORIGIN_WHITELIST = (
    'http://localhost',
    'localservername',
    'http://localservername',
    '127.0.0.1'
)


MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

但是,每当我点击 Rest Framework 提供的任何 API 时,我都会收到 No 'Access-Control-Allow-Origin' header is present on the requested resource. 错误。如果我设置 CORS_ORIGIN_ALLOW_ALL = True,那么 API 可以正常工作,但这对我的服务器端数据来说非常不安全。

我需要更改什么才能解决此问题?

根据 http://www.w3.org/Security/wiki/Same_Origin_Policy,请求应来自相同的端口、方案和主机,才能被视为同源。这里你的一台服务器在 80 端口,另一台在 8080 端口。

An origin is defined by the scheme, host, and port of a URL. Generally speaking, documents retrieved from distinct origins are isolated from each other. For example, if a document retrieved from http://example.com/doc.html tries to access the DOM of a document retrieved from https://example.com/target.html, the user agent will disallow access because the origin of the first document, (http, example.com, 80), does not match the origin of the second document (https, example.com, 443).

我遇到了同样的问题。通过浏览django-cors-headers-代码发现我的错误如下:

虽然完整的 CORS-header 看起来像这样(注意架构和主机名):

Access-Control-Allow-Origin: https://example.com

CORS_ORIGIN_WHITELIST 设置希望它的格式与 Origin-header 的 urlparse.netloc (docs) 相比,这只是主机(可能是端口)

def origin_found_in_white_lists(self, origin, url):
    return (
        url.netloc in conf.CORS_ORIGIN_WHITELIST or
        (origin == 'null' and origin in conf.CORS_ORIGIN_WHITELIST) or
        self.regex_domain_match(origin)
    )

虽然 RegEx-whitelist 将其与完整的 Origin-header 进行比较。

因此正确的设置(如 setup-manual 中的示例正确说明,但未正确描述)将是:

CORS_ORIGIN_WHITELIST = (
    'example.com',
)

如果您不希望您的 API 与网站的 non-secure http-version 对话,这可能是个问题。在这种情况下使用 RegEx。

另请注意:在故障排除期间,我发现如果未找到匹配项,则 CORS-header 完全不存在。这意味着缺少 header 并不能确定中间件完全故障,而可能只是配置错误。

也许大括号很重要

[] 而不是 ()

CORS_ORIGIN_WHITELIST = [

    'http://localhost',

    'localservername',

    'http://localservername',

    '127.0.0.1'

]

应该可以

大括号对我来说很重要,请使用 [] 而不是 (), 我已经在 Django 中验证过, 其他的我还没看。

这样写:

CORS_ORIGIN_WHITELIST=[ 'https://localhost:3000' ]

CORS_ORIGIN_WHITELIST=[ 'https://localhost:3000' ] 工作正常。

这对我有用,请检查一下,它可能会帮助您解决问题。

CORS_ORIGIN_WHITELIST = (
    'http://localhost',
)

在此错误中,提示清楚地提到它需要 https://

HINT: Add a scheme (e.g. https://) or netloc (e.g. example.com).

此外,大括号在 django 设置中也很重要。

CORS_ORIGIN_WHITELIST = [
    'https://localhost:3000'
]

以上设置工作正常。

虽然不同括号的相同设置不起作用

CORS_ORIGIN_WHITELIST = (
    'https://localhost:3000'
)

写的像 CORS_ORIGIN_WHITELIST = [ 'https://localhost:3000' ] 对我来说很好。

复制django-cors-headers documentation中的白名单代码,然后修改即可:

CORS_ORIGIN_WHITELIST = [
    "https://example.com",
    "https://sub.example.com",
    "http://localhost:8080",
    "http://127.0.0.1:9000"
]

这将保证您没有打错字。

对我来说,我使用 [] 而不是 ()。 也不要在末尾添加 '/' url.

像这样

CORS_ORIGIN_WHITELIST = [
    'http://localhost:3000'
]

确保不要添加路径 /,如下所示:

CORS_ORIGIN_WHITELIST = (
    'https://localhost:3000/'
)

只需添加以下代码即可。

CORS_ORIGIN_WHITELIST = [
    'https://localhost:3000'
]

我认为解决此错误的最佳方法是:-

  1. 在浏览器中输入 URL,例如:- 如果您是 运行 一个 angular 应用程序,那么

    服务

    并在浏览器中输入“ng serve”命令给出的 URL。例如:- localhost:4200

  2. 然后,从浏览器复制 URL 并按原样粘贴到 cors 允许的主机中。例如:-

    CORS_ALLOWED_ORIGINS = [ 'http://localhost:4200', ]

注意:- 不要忘记从 URL 中删除最后一个“/”符号,因此, http://localhost:4200/ 添加这个:- http://localhost:4200

在此之后,您将不会看到任何错误。