本文建立在你刚创建了Django项目并且还没做任何修改
settings.py
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
...
}
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5), # token有效期5分钟
'REFRESH_TOKEN_LIFETIME': timedelta(days=1) # 刷新后token有效期延长1天
}
urls.py
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
...
# 认证令牌
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
# 刷新令牌
path('api/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
...
]