我目前正在尝试使用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”标头。因此不允许访问源'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‘
当我在火狐或Postman上使用restclient与CORS_ORIGIN_ALLOW_ALL = False组合时,我没有得到任何错误
我用来调用接口的地址是'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',
)
}谢谢!
发布于 2016-01-15 15:58:01
使用CORS头部,您可以限制允许哪些客户端发出请求,以及允许哪些方法发出请求。
访问-控制-允许-来源:http://siteA.com
访问控制允许方法: GET,POST,PUT
还有其他的标题,Google it :)
或者,Angular是在Apache或Node上运行的吗?如果是,那么您可以向相同的域发出请求,例如http://yourangulardomain.com/api/request/that/i/want/to/go/to/my/django/server
然后在Apache/Node配置中添加重写规则以重写请求。这将绕过交叉原点问题。
在Node (为Angular提供服务)上使用npm模块connect-modrewrite (主要基于Apache重写规则)的类似重写规则是...
middleware: [
rewrite([
'^/api/(.*)$ http://10.20.1.20:9100/$1 [P]',
'^[^\\.]*$ /index.html [L]'
])
]这基本上是将在URL中包含/api的请求发送到diff服务器,但将其他所有内容路由到index.html
我不确定为什么这不会影响对CSS文件和类似文件的请求!
无论如何,希望这有助于指导您:)
https://stackoverflow.com/questions/34802715
复制相似问题