当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

2018个人年度工作总结与2019工作计划(互联网)

快快使用ModelArts,零基礎小白也能玩轉AI!

如何对Pandas DataFrame进行自定义排序

Vue 3 responsive Foundation

hadoop 命令总结

至联云分享:IPFS/Filecoin值不值得投资?

连肝三个通宵,JVM77道高频面试题详细分析,就这?

Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things

Pycharm快捷键 自定义功能形式

在大规模 Kubernetes 集群上实现高 SLO 的方法
随机推荐
中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
A debate on whether flv should support hevc
Basic principle and application of iptables
hadoop 命令总结
Python自动化测试学习哪些知识?
Network programming NiO: Bio and NiO
【效能優化】納尼?記憶體又溢位了?!是時候總結一波了!!
自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
深度揭祕垃圾回收底層,這次讓你徹底弄懂她
After brushing leetcode's linked list topic, I found a secret!
EOS创始人BM: UE,UBI,URI有什么区别?
你的财务报告该换个高级的套路了——财务分析驾驶舱
事半功倍:在没有机柜的情况下实现自动化
制造和新的自动化技术是什么?
Polkadot series (2) -- detailed explanation of mixed consensus
Elasticsearch database | elasticsearch-7.5.0 application construction
连肝三个通宵,JVM77道高频面试题详细分析,就这?
PLC模拟量输入和数字量输入是什么
Elasticsearch 第六篇:聚合統計查詢
恕我直言,我也是才知道ElasticSearch条件更新是这么玩的