当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
- Subordination judgment in structured data
- 使用 Iceberg on Kubernetes 打造新一代云原生数据湖
- 一时技痒,撸了个动态线程池,源码放Github了
- Sort the array in ascending order according to the frequency
- Programmer introspection checklist
- EOS创始人BM: UE,UBI,URI有什么区别?
- Kitty中的动态线程池支持Nacos,Apollo多配置中心了
- 关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
- 【快速因數分解】Pollard's Rho 演算法
猜你喜欢
读取、创建和运行多个文件的3个Python技巧
Computer TCP / IP interview 10 even asked, how many can you withstand?
哇,ElasticSearch多字段权重排序居然可以这么玩
100元扫货阿里云是怎样的体验?
Tool class under JUC package, its name is locksupport! Did you make it?
Basic principle and application of iptables
事半功倍:在没有机柜的情况下实现自动化
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
(1)ASP.NET Core3.1 Ocelot介紹
随机推荐
加速「全民直播」洪流,如何攻克延时、卡顿、高并发难题?
制造和新的自动化技术是什么?
车的换道检测
Flink的DataSource三部曲之二:内置connector
你的财务报告该换个高级的套路了——财务分析驾驶舱
自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
Calculation script for time series data
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
技術總監,送給剛畢業的程式設計師們一句話——做好小事,才能成就大事
PLC模拟量输入和数字量输入是什么
html
容联完成1.25亿美元F轮融资
Computer TCP / IP interview 10 even asked, how many can you withstand?
Top 10 best big data analysis tools in 2020
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
DTU连接经常遇到的问题有哪些
Leetcode's ransom letter
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
03_ Detailed explanation and test of installation and configuration of Ubuntu Samba
词嵌入教程