1、settings中配置前端
AUTHENTICATION_BACKENDS = ( 'users.views.CustomBackend', )
2、users/views.pydjango
# users.views.py from django.contrib.auth.backends import ModelBackend from django.contrib.auth import get_user_model from django.db.models import Q User = get_user_model() class CustomBackend(ModelBackend): """ 自定義用戶驗證 """ def authenticate(self, username=None, password=None, **kwargs): try: #用戶名和手機都能登陸 user = User.objects.get( Q(username=username) | Q(mobile=username)) if user.check_password(password): return user except Exception as e: return None
3、JWT有效時間設置spa
settings中配置code
import datetime #有效期限 JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7), #也能夠設置seconds=20 'JWT_AUTH_HEADER_PREFIX': 'JWT', #JWT跟前端保持一致,好比「token」這裏設置成JWT }