当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- ThreadLocal原理大解析
- (2)ASP.NET Core3.1 Ocelot路由
- Top 10 best big data analysis tools in 2020
- 【新閣教育】窮學上位機系列——搭建STEP7模擬環境
- GBDT与xgb区别,以及梯度下降法和牛顿法的数学推导
- Can't be asked again! Reentrantlock source code, drawing a look together!
- Tool class under JUC package, its name is locksupport! Did you make it?
- 幽默:黑客式编程其实类似机器学习!
- Dapr實現分散式有狀態服務的細節
- Programmer introspection checklist
猜你喜欢
随机推荐
Top 10 best big data analysis tools in 2020
如何对Pandas DataFrame进行自定义排序
Group count - word length
有关PDF417条码码制的结构介绍
谁说Cat不能做链路跟踪的,给我站出来
自然语言处理之命名实体识别-tanfordcorenlp-NER(一)
前端模組化簡單總結
[C#] (原創)一步一步教你自定義控制元件——04,ProgressBar(進度條)
100元扫货阿里云是怎样的体验?
OPTIMIZER_ Trace details
Why do private enterprises do party building? ——Special subject study of geek state holding Party branch
PHPSHE 短信插件说明
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
Serilog原始碼解析——使用方法
03_ Detailed explanation and test of installation and configuration of Ubuntu Samba
Working principle of gradient descent algorithm in machine learning
C language 100 question set 004 - statistics of the number of people of all ages
微服務 - 如何解決鏈路追蹤問題
Pycharm快捷键 自定义功能形式
Didi elasticsearch cluster cross version upgrade and platform reconfiguration







