当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- PHPSHE 短信插件说明
- Can't be asked again! Reentrantlock source code, drawing a look together!
- Sort the array in ascending order according to the frequency
- 采购供应商系统是什么?采购供应商管理平台解决方案
- 如果前端不使用SPA又能怎样?- Hacker News
- Python自动化测试学习哪些知识?
- Asp.Net Core learning notes: Introduction
- Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
- Tool class under JUC package, its name is locksupport! Did you make it?
- 免费的专利下载教程(知网、espacenet强强联合)
猜你喜欢

Network programming NiO: Bio and NiO

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

全球疫情加速互联网企业转型,区块链会是解药吗?

Pycharm快捷键 自定义功能形式

How long does it take you to work out an object-oriented programming interview question from Ali school?

Tool class under JUC package, its name is locksupport! Did you make it?

Didi elasticsearch cluster cross version upgrade and platform reconfiguration

分布式ID生成服务,真的有必要搞一个

DRF JWT authentication module and self customization

Can't be asked again! Reentrantlock source code, drawing a look together!
随机推荐
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
幽默:黑客式编程其实类似机器学习!
Pattern matching: The gestalt approach一种序列的文本相似度方法
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
微服務 - 如何解決鏈路追蹤問題
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
C++和C++程序员快要被市场淘汰了
3分钟读懂Wi-Fi 6于Wi-Fi 5的优势
神经网络简史
Every day we say we need to do performance optimization. What are we optimizing?
Tool class under JUC package, its name is locksupport! Did you make it?
X Window System介紹
DTU连接经常遇到的问题有哪些
Chainlink将美国选举结果带入区块链 - Everipedia
分布式ID生成服务,真的有必要搞一个
“颜值经济”的野望:华熙生物净利率六连降,收购案遭上交所问询
用Keras LSTM构建编码器-解码器模型
Calculation script for time series data
Introduction to Google software testing
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置