Django-rest-auth 和 AWS - 错误 401
Posted
技术标签:
【中文标题】Django-rest-auth 和 AWS - 错误 401【英文标题】:Django-rest-auth and AWS - Error 401 【发布时间】:2018-12-30 20:45:59 【问题描述】:在我的 DRF 应用程序中,我使用包 Django-rest-auth 进行 JWT 身份验证。 在本地环境中一切正常:我正在登录并获取令牌。令牌已添加到请求的标头中,并且数据已获取。 但是在 AWS 上部署后,当我尝试登录时,我得到了一个正确的令牌(我猜),当我将他添加到标题中时,我得到一个错误 401。 有人知道怎么修吗?
views.py
from django.db.models import Count
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from rest_framework.renderers import JSONRenderer
from rest_framework.decorators import action
from rest_framework.response import Response
from .models import Task
from .serializers import TaskSerializer, TaskCreateSerializer
from .filters import TaskFilter
class TaskViewSet(ModelViewSet):
permission_classes = (IsAuthenticated, )
renderer_classes = (JSONRenderer,)
filterset_class = TaskFilter
def get_queryset(self):
return Task.objects.all()\
.select_related('owner')\
.only('name', 'owner__username', 'content', 'finished', 'start')
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def get_serializer_class(self):
if self.request.method == 'POST':
return TaskCreateSerializer
else:
return TaskSerializer
@action(methods=['GET'], detail=False)
def dates(self, request, *args, **kwargs):
qs = self.get_queryset()\
.extra(select='start': 'Date(start)')\
.values('start')\
.annotate(tasks=Count('start'))
return Response(qs, 200)
settings.py
"""
Django settings for MyToDo project.
Generated by 'django-admin startproject' using Django 2.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
import datetime
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '<enter-your-secret-key-here>'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# Third party packages
'rest_framework',
'rest_framework.authtoken',
'django_filters',
'allauth',
'allauth.account',
'rest_auth',
'rest_auth.registration',
# 'rest_framework_cache',
# For development
'debug_toolbar',
# My app
'tasks',
'users',
]
SITE_ID = 1
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'MyToDo.urls'
AUTH_USER_MODEL = 'users.User'
INTERNAL_IPS = '127.0.0.1:8000'
# CORS
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
# REST FRAMEWORK
REST_FRAMEWORK =
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DATETIME_FORMAT': "%d/%m/%Y %H:%M:%S",
# REST AUTH
OLD_PASSWORD_FIELD_ENABLED = True
REST_AUTH_SERIALIZERS =
'USER_DETAILS_SERIALIZER': 'users.serializers.UserSerializer',
# JWT OPTIONS
REST_USE_JWT = True
JWT_AUTH =
'JWT_ENCODE_HANDLER':
'rest_framework_jwt.utils.jwt_encode_handler',
'JWT_DECODE_HANDLER':
'rest_framework_jwt.utils.jwt_decode_handler',
'JWT_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_payload_handler',
'JWT_PAYLOAD_GET_USER_ID_HANDLER':
'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',
'JWT_RESPONSE_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_response_payload_handler',
'JWT_SECRET_KEY': SECRET_KEY,
'JWT_GET_USER_SECRET_KEY': None,
'JWT_PUBLIC_KEY': None,
'JWT_PRIVATE_KEY': None,
'JWT_ALGORITHM': 'HS256',
'JWT_VERIFY': True,
'JWT_VERIFY_EXPIRATION': True,
'JWT_LEEWAY': 0,
'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_AUDIENCE': None,
'JWT_ISSUER': None,
'JWT_ALLOW_REFRESH': True,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_AUTH_HEADER_PREFIX': 'JWT',
'JWT_AUTH_COOKIE': None,
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 = 'MyToDo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
if 'RDS_DB_NAME' in os.environ:
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
else:
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'localtask',
'USER': 'localtask',
'PASSWORD': 'localtask',
'HOST': 'localhost',
'PORT': '',
# Cache
CACHES =
"default":
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
"OPTIONS":
"CLIENT_CLASS": "django_redis.client.DefaultClient",
# REST Cache
REST_FRAMEWORK_CACHE =
'DEFAULT_CACHE_BACKEND': 'default',
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
,
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
,
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
,
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
,
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/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/2.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "www", "static")
python.config
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "MyToDo.settings"
PYTHONPATH: "$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: "MyToDo/wsgi.py"
StaticFiles: "/static/=www/static/"
packages:
yum:
postgresql95-devel: []
container_commands:
01_makemigrations:
command: "python manage.py makemigrations users tasks"
leader_only: true
02_migrate:
command: "python manage.py migrate"
leader_only: true
03_collectstatic:
command: "python manage.py collectstatic --noinput"
04_createsu:
command: "python manage.py create_user <username> <password> <email> <1 - if superuser>"
【问题讨论】:
测试AWS时如何添加auth header? 我在测试期间在 Postman 中添加了标题。 【参考方案1】:我的猜测是服务器不允许授权标头。
尝试将此添加到您的容器命令中:
01_wsgipass:
command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
【讨论】:
以上是关于Django-rest-auth 和 AWS - 错误 401的主要内容,如果未能解决你的问题,请参考以下文章
有啥方法可以更改登录的 Django-rest-auth 视图?
使用 django-rest-auth 和 allauth 进行 Google 身份验证
带有 django-rest-auth 和 django-rest-knox 的 AttributeError - 令牌序列化器
我可以通过 django-rest-auth 登录和注册返回 JSON Web Token (JWT) 吗?