当前位置:网站首页>Process analysis of Python authentication mechanism based on JWT
Process analysis of Python authentication mechanism based on JWT
2020-11-06 01:16:00 【B4713c Football Club】
1.jwt Advantages and disadvantages
jwt The advantages of :
1. It's very convenient to implement distributed single sign on
2. The data is actually stored on the client side , So we can share the storage pressure of database or server
jwt The shortcomings of :
1. The data is saved on the client side , We only recognize jwt, The client is not recognized . 2. jwt You can set the expiration time , But because the data is stored on the client side , So it's not easy to adjust the expiration time .
2. install jwt
pip install djangorestframework-jwt -i https://pypi.douban.com/simple
3. stay settings.dev in
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.BasicAuthentication', ), } import datetime JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), ## Set up token Effective value }
4. Generate... Manually jwt
from rest_framework_jwt.settings import api_settings jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload)
5. The back end implements the login authentication interface ( In sub application routing urls.py in )
from rest_framework_jwt.views import obtain_jwt_token urlpatterns = [ path(r'login/', obtain_jwt_token), ## Provide the interface ]
6. Custom return data ,(user.utils.jwt_response_payload_handler Under the path )
def jwt_response_payload_handler(token, user=None, request=None): """ Customize jwt Authentication successful return data """ return { 'token': token, 'id': user.id, 'username': user.username }# This is custom jwt Authentication successful return data , It's usually placed under sub applications utils In file , And then in settings Middle configuration , Tell the path django
7. modify settings.dev The configuration file
# JWT JWT_AUTH = { 'JWT_EXPIRATION_DELTA': datetime.timedelta(days=1), 'JWT_RESPONSE_PAYLOAD_HANDLER': 'user.utils.jwt_response_payload_handler', }
The above is the whole content of this paper , I hope it will be helpful for your study , I also hope that you can support .
版权声明
本文为[B4713c Football Club]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢
Using Es5 to realize the class of ES6
Network programming NiO: Bio and NiO
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
全球疫情加速互联网企业转型,区块链会是解药吗?
2018个人年度工作总结与2019工作计划(互联网)
Jmeter——ForEach Controller&Loop Controller
python过滤敏感词记录
Working principle of gradient descent algorithm in machine learning
如何对Pandas DataFrame进行自定义排序
业内首发车道级导航背后——详解高精定位技术演进与场景应用
随机推荐
xmppmini 專案詳解:一步一步從原理跟我學實用 xmpp 技術開發 4.字串解碼祕笈與訊息包
Using Es5 to realize the class of ES6
Asp.Net Core學習筆記:入門篇
神经网络简史
关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
Programmer introspection checklist
用Python构建和可视化决策树
Cocos Creator 原始碼解讀:引擎啟動與主迴圈
【效能優化】納尼?記憶體又溢位了?!是時候總結一波了!!
Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
Chainlink将美国选举结果带入区块链 - Everipedia
Dapr實現分散式有狀態服務的細節
Network programming NiO: Bio and NiO
給萌新HTML5 入門指南(二)
通过深层神经网络生成音乐
GUI 引擎评价指标
Microservices: how to solve the problem of link tracing
The practice of the architecture of Internet public opinion system
词嵌入教程
Pycharm快捷键 自定义功能形式