当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 用Keras LSTM构建编码器-解码器模型
- 词嵌入教程
- 在大规模 Kubernetes 集群上实现高 SLO 的方法
- Chainlink将美国选举结果带入区块链 - Everipedia
- C language 100 question set 004 - statistics of the number of people of all ages
- X Window System介紹
- Can't be asked again! Reentrantlock source code, drawing a look together!
- 使用NLP和ML来提取和构造Web数据
- Skywalking series blog 2-skywalking using
- Dapr實現分散式有狀態服務的細節
猜你喜欢

文本去重的技术方案讨论(一)

数据产品不就是报表吗?大错特错!这分类里有大学问

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

Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)

直播预告 | 微服务架构学习系列直播第三期

使用 Iceberg on Kubernetes 打造新一代云原生数据湖

DRF JWT authentication module and self customization

Working principle of gradient descent algorithm in machine learning

一时技痒,撸了个动态线程池,源码放Github了

向北京集结!OpenI/O 2020启智开发者大会进入倒计时
随机推荐
速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
怎么理解Python迭代器与生成器?
Real time data synchronization scheme based on Flink SQL CDC
2018个人年度工作总结与2019工作计划(互联网)
Flink的DataSource三部曲之二:内置connector
谁说Cat不能做链路跟踪的,给我站出来
网络安全工程师演示:原来***是这样获取你的计算机管理员权限的!【维持】
微服務 - 如何解決鏈路追蹤問題
Chainlink将美国选举结果带入区块链 - Everipedia
读取、创建和运行多个文件的3个Python技巧
Dapr實現分散式有狀態服務的細節
TRON智能钱包PHP开发包【零TRX归集】
Vue 3 responsive Foundation
03_ Detailed explanation and test of installation and configuration of Ubuntu Samba
Examples of unconventional aggregation
GUI 引擎评价指标
Want to do read-write separation, give you some small experience
The difference between Es5 class and ES6 class
drf JWT認證模組與自定製
How to select the evaluation index of classification model