我的请求是跨源请求吗?(Django rest api on heroku with CORS 不会阻止我的请求)
Is my request a cross origin request?(Django rest api on heroku with CORS not blocking my request)
我在 heroku 上托管了一个 django rest api 并在其中使用了 django-cors-headers。我已将一些 URL 添加到 CORS 白名单,但发现该应用正在接受来自任何来源的请求(例如:- 我的本地 PC)。我的问题是,为什么 heroku 没有阻止我的 http 请求,即使它没有被列入白名单。
注意:- 我没有任何前端应用程序运行
下面是我的快照 settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
)
MIDDLEWARE_CLASSES = (
'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.security.SecurityMiddleware',
'django.middleware.common.BrokenLinkEmailsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
# REST FRAMEWORK
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
],
# Make the default renderer class JSON when in production to prevent users from using the browsable API
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
]
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR)],
'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',
],
},
},
]
DATABASES = {'default': dj_database_url.config(default=os.environ["HEROKU_POSTGRESQL_RED_URL"])}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
#'localhost:9000',
#'localhost:5000',
#'127.0.0.1:9000',
#'127.0.0.1:5000',
)
CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
CORS_ALLOW_HEADERS = (
'Access-Control-Allow-Origin',
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
我希望它能像我在 settings.py 中使用 ALLOWED_HOSTS 时那样工作
ALLOWED_HOSTS = ['whitelist url']
如果我在这里遗漏了一些设置,请告诉我。
我觉得CORS_ALLOW_HEADERS应该是
CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
如果您使用任何缓存控制,您还应该添加缓存控制。
更新
如果您将应用程序托管在外部服务器上并尝试在本地访问它会怎样?
回答:当您的本地计算机访问互联网时,它会保留 ISP 分配的可用 IP。这意味着当外部服务器被您的机器击中时,它不会将其视为 127.0.0.1,而是将其视为此处 https://www.whatismyip.com/.
解决方案:要允许来自本地计算机的 cors 源,请执行以下操作之一,
将 https://www.whatismyip.com/ 上出现的内容添加到 CORS_ORIGIN_WHITELIST。但这意味着每次重新启动本地路由器时都必须编辑 CORS_ORIGIN_WHITELIST。这是因为重新启动本地路由器,ISP 会分配给您另一个 IP。
仅供测试,将'*'添加到CORS_ORIGIN_WHITELIST。
尝试使用另一个 heroku 实例来发出请求。
我在 heroku 上托管了一个 django rest api 并在其中使用了 django-cors-headers。我已将一些 URL 添加到 CORS 白名单,但发现该应用正在接受来自任何来源的请求(例如:- 我的本地 PC)。我的问题是,为什么 heroku 没有阻止我的 http 请求,即使它没有被列入白名单。
注意:- 我没有任何前端应用程序运行
下面是我的快照 settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
)
MIDDLEWARE_CLASSES = (
'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.security.SecurityMiddleware',
'django.middleware.common.BrokenLinkEmailsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
)
# REST FRAMEWORK
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
# 'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
# Use hyperlinked styles by default.
# Only used if the `serializer_class` attribute is not set on a view.
'DEFAULT_MODEL_SERIALIZER_CLASS':
'rest_framework.serializers.HyperlinkedModelSerializer',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.AllowAny',
],
# Make the default renderer class JSON when in production to prevent users from using the browsable API
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
]
}
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR)],
'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',
],
},
},
]
DATABASES = {'default': dj_database_url.config(default=os.environ["HEROKU_POSTGRESQL_RED_URL"])}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
#'localhost:9000',
#'localhost:5000',
#'127.0.0.1:9000',
#'127.0.0.1:5000',
)
CORS_ALLOW_METHODS = (
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
)
CORS_ALLOW_HEADERS = (
'Access-Control-Allow-Origin',
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
我希望它能像我在 settings.py 中使用 ALLOWED_HOSTS 时那样工作 ALLOWED_HOSTS = ['whitelist url']
如果我在这里遗漏了一些设置,请告诉我。
我觉得CORS_ALLOW_HEADERS应该是
CORS_ALLOW_HEADERS = (
'x-requested-with',
'content-type',
'accept',
'origin',
'authorization',
'x-csrftoken'
)
如果您使用任何缓存控制,您还应该添加缓存控制。
更新
如果您将应用程序托管在外部服务器上并尝试在本地访问它会怎样?
回答:当您的本地计算机访问互联网时,它会保留 ISP 分配的可用 IP。这意味着当外部服务器被您的机器击中时,它不会将其视为 127.0.0.1,而是将其视为此处 https://www.whatismyip.com/.
解决方案:要允许来自本地计算机的 cors 源,请执行以下操作之一,
将 https://www.whatismyip.com/ 上出现的内容添加到 CORS_ORIGIN_WHITELIST。但这意味着每次重新启动本地路由器时都必须编辑 CORS_ORIGIN_WHITELIST。这是因为重新启动本地路由器,ISP 会分配给您另一个 IP。
仅供测试,将'*'添加到CORS_ORIGIN_WHITELIST。
尝试使用另一个 heroku 实例来发出请求。