【问题标题】:JWT Token expiration time increaseJWT Token过期时间增加
【发布时间】:2018-11-19 12:22:38
【问题描述】:

我已将 JWT 令牌与 django-restframwork 集成,在这里我设置了过期时间 15 分钟 JWT_EXPIRATION_DELTA 但它在提到的时间(1 分钟)之前过期,我需要刷新令牌才能继续... PFB me 配置

Python 3.5
Django==2.0.5
djangorestframework==3.8.2
djangorestframework-simplejwt==3.2.3

设置.py

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    )
} 

JWT_AUTH = {
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=900),
    'JWT_ALLOW_REFRESH': True,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
}

请帮忙,我这里有什么错误吗。

【问题讨论】:

    标签: django python-3.x django-rest-framework jwt


    【解决方案1】:

    您仍然可以使用 djangorestframework-simplejwt 并改为修改设置文件中的时间

    settings.py

    from datetime import timedelta
    
    ...
    
    SIMPLE_JWT = {
        '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,
    
        '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',
    
        'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp',
        'SLIDING_TOKEN_LIFETIME': timedelta(minutes=5),
        'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1),
    }
    

    退房 https://github.com/davesque/django-rest-framework-simplejwt

    【讨论】:

      【解决方案2】:

      您使用的是djangorestframework-simplejwt,但您的配置是针对djangorestframework-jwt。请卸载djangorestframework-simplejwt,然后安装djangorestframework-jwt

      pip uninstall djangorestframework-simplejwt

      然后

      pip install djangorestframework-jwt 更多详情here

      【讨论】: