资料1:https://blog.csdn.net/kaikai0803/article/details/98766036
资料2:https://blog.csdn.net/weixin_42134789/article/details/109554843
pip install djangorestframework_simplejwt
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', # 生成drf自动文档
# settings配置全局认证,配置登录
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication', # djangorestframework_simplejwt JWT认证
'rest_framework.authentication.BasicAuthentication', # Basic认证(账号密码)
'rest_framework.authentication.SessionAuthentication', # Session认证
]
}
# 如果要覆盖Simple JWT的默认设置。
from datetime import timedelta
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=1), # 配置ACCESS过期时间
'REFRESH_TOKEN_LIFETIME': timedelta(days=15), # 配置REFRESH过期时间
'ROTATE_REFRESH_TOKENS': True,
}
JWT设置(Simple JWT的默认设置)
# JWT设置(官网给出的全部配置项)
DEFAULTS = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
'ALGORITHM': 'HS256',
'SIGNING_KEY': settings.SECRET_KEY,
'VERIFYING_KEY': None,
'AUDIENCE': None,
'ISSUER': None,
'AUTH_HEADER_TYPES': ('Bearer',),
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
'TOKEN_TYPE_CLAIM': 'token_type',
'JTI_CLAIM': 'jti',
'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
'SLIDING_TOKEN_LIFETIME': timedelta(minutes=5),
'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1),
}
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
urlpatterns = [
# jwt的认证接口
path('login/', TokenObtainPairView.as_view(), name='token_obtain_pair'),# 登录一般是login
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), # simplejwt认证接口
]
http://127.0.0.1:8000/login/
http://127.0.0.1:8000/api/token/refresh/