当前位置:网站首页>Restframework-simplejwt rewrite authentication mechanism
Restframework-simplejwt rewrite authentication mechanism
2022-07-01 06:14:00 【Ink seeking Roy】
from rest_framework_simplejwt.authentication import JWTAuthentication
class MyJWTAuthentication(JWTAuthentication):
''' modify JWT Certification , Return to custom User Table object '''
def authenticate(self, request):
header = self.get_header(request)
if header is None:
return None
# obtain access_token
raw_token = self.get_raw_token(header)
user = None
token = None
if raw_token is None:
return None
# Define the current access_token White list
wraw_token = str(raw_token, 'utf-8') + config.JWT_WHITE_LIST_TAG
# When there is a When in the white list
if cache.has_key(wraw_token):
# In the cache obtain stay middleware Set up User object
user = cache.get(wraw_token)["user_obj"]
token = raw_token
else:
try:
# Yes, I received access_token authentication
token = self.get_validated_token(raw_token)
# According to the certified access_token Get user object
user = self.get_user(token)
except Exception as e:
# Ordinary access_token Exceptions are not handled How to throw a response is to throw a response
# primary access_token abnormal , When refreshing the interface for , obtain request.data Of refresh_token
if request.data.get("refresh_token"):
refresh_token = request.data.get("refresh_token")
# Get current refresh_token Of Valid time
refresh_time_remaining = output_time_remaining(refresh_token)
# refresh_token Within the validity period
if refresh_time_remaining > 0:
# And not on the blacklist
if not cache.has_key(refresh_token):
decoded_data = jwt_decode(str(refresh_token), config.JWT_SECRET_KEY, verify=False,
algorithms="HS256")
# obtain User object
user = Person_info.objects.get(**{
'person_id': decoded_data["user_id"]})
return user, token
def get_user(self, validated_token):
try:
# Whether it has been entered into the blacklist
if cache.has_key(str(validated_token) + config.JWT_BLACK_LIST_TAG):
raise AuthenticationFailed(_('Token contained no recognizable user identification'))
user_id = validated_token['user_id']
except KeyError:
raise InvalidToken(_('Token contained no recognizable user identification'))
try:
user = Person_info.objects.get(**{
'person_id': user_id})
except Person_info.DoesNotExist:
raise AuthenticationFailed(_('User not found'), code='user_not_found')
return user
边栏推荐
- Arcserver password reset (account cannot be reset)
- Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
- 利用百度地图查询全国地铁线路
- C# ManualResetEvent 类的理解
- ArcServer密码重置(账号不可以重置)
- Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
- 数据库问题,如何优化Oracle SQL查询语句更快,效率更高
- 3D打印机穿线:5种简单的解决方案
- Index method and random forest to realize the information of surface water body in wet season in Shandong Province
- excel可视化
猜你喜欢
随机推荐
图片服务器项目测试
Elements of database ER diagram
高阶-二叉平衡树
FPGA - 7系列 FPGA内部结构之Clocking -01- 时钟架构概述
excel可视化
Small guide for rapid completion of mechanical arm (VI): stepping motor driver
Retention rate of SQL required questions
pycharm 配置jupyter
记磁盘扇区损坏导致的Mysql故障排查
Stack Title: parsing Boolean expressions
【ManageEngine卓豪】局域网监控的作用
kotlin位运算的坑(bytes[i] and 0xff 报错)
Index method and random forest to realize the information of surface water body in wet season in Shandong Province
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
69 cesium code datasource loading geojson
【网络安全工具】USB控制软件有什么用
TiDB单机模拟部署生产环境集群(闭坑实践,亲测有效)
Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432
交换机配置软件具有的作用
Database problems, how to optimize Oracle SQL query statements faster and more efficient






![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)

