本文建立在你刚创建了Django项目并且还没做任何修改

settings.py

  1. REST_FRAMEWORK = {
  2. ...
  3. 'DEFAULT_AUTHENTICATION_CLASSES': (
  4. ...
  5. 'rest_framework_simplejwt.authentication.JWTAuthentication',
  6. )
  7. ...
  8. }
  9. SIMPLE_JWT = {
  10. 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5), # token有效期5分钟
  11. 'REFRESH_TOKEN_LIFETIME': timedelta(days=1) # 刷新后token有效期延长1天
  12. }

urls.py

  1. from rest_framework_simplejwt.views import (
  2. TokenObtainPairView,
  3. TokenRefreshView,
  4. )
  5. urlpatterns = [
  6. ...
  7. # 认证令牌
  8. path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
  9. # 刷新令牌
  10. path('api/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
  11. ...
  12. ]