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

嘘!异步事件这样用真的好么?

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

助力金融科技创新发展,ATFX走在行业最前列

TRON智能钱包PHP开发包【零TRX归集】

用Keras LSTM构建编码器-解码器模型

速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)

3分钟读懂Wi-Fi 6于Wi-Fi 5的优势

(1)ASP.NET Core3.1 Ocelot介紹

Jmeter——ForEach Controller&Loop Controller

关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
随机推荐
Subordination judgment in structured data
网络安全工程师演示:原来***是这样获取你的计算机管理员权限的!【维持】
給萌新HTML5 入門指南(二)
哇,ElasticSearch多字段权重排序居然可以这么玩
How to get started with new HTML5 (2)
(1)ASP.NET Core3.1 Ocelot介紹
Query意图识别分析
一时技痒,撸了个动态线程池,源码放Github了
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
After brushing leetcode's linked list topic, I found a secret!
Ubuntu18.04上安裝NS-3
Asp.Net Core learning notes: Introduction
GBDT与xgb区别,以及梯度下降法和牛顿法的数学推导
简直骚操作,ThreadLocal还能当缓存用
多机器人行情共享解决方案
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
hadoop 命令总结
Listening to silent words: hand in hand teaching you sign language recognition with modelarts
(1) ASP.NET Introduction to core3.1 Ocelot
恕我直言,我也是才知道ElasticSearch条件更新是这么玩的