安装
pip3 install djangorestframework-jwt
全局配置
1、 settings.py
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
2、 根路由
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns += [
path('jwt-token-auth/',obtain_jwt_token ),
]
3、 设置 Tocken 有效时间和认证 token 信息的前缀
在settings.py文件配置
import datetime
JWT_AUTH = {
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_AUTH_HEADER_PREFIX': 'JWT',
}
局部配置与官方版类似,这里就不专门说明了
postman软件版操作
![](https://img.haomeiwen.com/i18861860/8d59d108afdc2a24.png)
![](https://img.haomeiwen.com/i18861860/4318c210501e0ff5.png)
网友评论