- Python(3.7、3.8、3.9)
- Django (2.2, 3.1, 3.2)
- Django REST 框架(3.10、3.11、3.12)
使用 pip 安装简单的 JWT:
pip install djangorestframework-simplejwt
然后,django 项目settings.py中,添加 rest_framework_simplejwt.authentication.JWTAuthentication到身份验证类列表中:
REST_frameWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ) }配置
在django项目的url.py文件中对视图路由进行配置
from rest_framework_simplejwt.views import TokenObtainPairView,TokenRefreshView urlpatterns = [ ... path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), ... ]本地调试
将simplejwt配置到INSTALLED_APPS
INSTALLED_APPS = [ 'rest_framework_simplejwt',测试
使用postman进行访问调试
利用django第三方库很快的完成系统的鉴权机制,人生苦短,我用python!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)