【发布时间】:2016-04-20 13:17:34
【问题描述】:
我目前正在尝试使用 Django 作为后端使用 AngularJS 创建一个 Web 界面,但我得到了常见的 CORS 错误:XMLHttpRequest 无法加载 http://fatboyapi.ddns.net:8000/o/revoke_token/?client_id=xxx&client_secret=xxx&token=xxxxxxxx。请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://fatboy.ddns.net:8000' 因此不允许访问..
当 CORS_ORIGIN_ALLOW_ALL 标志设置为 True 时,一切正常,但显然不安全。我调用的终点是 django-oauth-toolkit 提供的 o/token/
我将这些添加到我的白名单的链接中。 'http://fatboyapi.ddns.net:8000', 'http://fatboy.ddns.net:8000'
当我在 firefox 或 Postman 上使用 restclient 并结合 CORS_ORIGIN_ALLOW_ALL = False 时,我没有收到任何错误
我用来调用我的api的地址是'http://fatboy.ddns.net:8000'
这是我在 Django 中使用的包
boto==2.38.0
contextlib2==0.4.0
Django==1.9
django-braces==1.8.1
django-cors-headers==1.1.0
django-custom-user==0.5
django-debug-toolbar==1.4
django-guardian==1.3.2
django-indexer==0.3.0
django-oauth-toolkit==0.9.0
django-paging==0.2.5
django-storages==1.1.8
django-templatetag-sugar==1.0
djangorestframework==3.3.1
docutils==0.12
eventlet==0.17.4
greenlet==0.4.9
lockfile==0.12.2
oauthlib==1.0.1
Pillow==2.9.0
psycopg2==2.6.1
six==1.10.0
sqlparse==0.1.18
wheel==0.24.0
这是我的 settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'custom_user',
'guardian',
'rest_framework',
'oauth2_provider',
'scheduleauthentication',
'punchclock',
'debug_toolbar',
)
AUTH_USER_MODEL = 'custom_user.EmailUser'
ANONYMOUS_USER_ID = -1
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
)
MIDDLEWARE_CLASSES = (
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'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',
'django.middleware.security.SecurityMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
ROOT_URLCONF = 'punchclock.urls'
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
'https://fatboyapi.ddns.net',
'https://fatboy.ddns.net',
'http://fatboyapi_i.ddns.net',
'http://fatboy_i.ddns.net',
'http://fatboyapi.ddns.net:8000',
'http://fatboy.ddns.net:8000'
)
CORS_ALLOW_CREDENTIALS = False
CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'punchclock.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'NAME': 'pc',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost',
'USER': 'postgres',
'PASSWORD': 'fatboy',
'PORT': '5432',
}
}
#REST-FRAMEWORK
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.ext.rest_framework.OAuth2Authentication',
)
}
谢谢!
【问题讨论】:
-
有什么问题?你说它在“Firefox 和 Postman 结合......”中工作:)
-
我想将:CORS_ORIGIN_ALLOW_ALL 设置为 FALSE,并让我的应用程序在没有 CORS 错误的情况下运行。因为有了这个设置,它只适用于邮递员,它不在我的应用程序中(我得到了我在问题开头写的错误)。
标签: javascript python angularjs django cors