JWT令牌到期时间增加

时间:2018-06-09 19:07:26

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

我已经将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

Setting.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),
}

请帮助,我在这里犯了任何错误。

2 个答案:

答案 0 :(得分:2)

您使用的是djangorestframework-simplejwt,但您的配置适用于djangorestframework-jwt。请卸载djangorestframework-simplejwt并安装djangorestframework-jwt

pip uninstall djangorestframework-simplejwt

然后

pip install djangorestframework-jwt 更多详情here

答案 1 :(得分: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