当前位置:网站首页>restframework-simpleJWT重写认证机制
restframework-simpleJWT重写认证机制
2022-07-01 05:45:00 【寻墨roy】
from rest_framework_simplejwt.authentication import JWTAuthentication
class MyJWTAuthentication(JWTAuthentication):
''' 修改JWT认证类,返回自定义User表对象 '''
def authenticate(self, request):
header = self.get_header(request)
if header is None:
return None
# 获取 access_token
raw_token = self.get_raw_token(header)
user = None
token = None
if raw_token is None:
return None
# 定义当前 access_token白名单
wraw_token = str(raw_token, 'utf-8') + config.JWT_WHITE_LIST_TAG
# 当出现 在白名单中时
if cache.has_key(wraw_token):
# 在缓存中 获取 在 中间件 设置的 用户对象
user = cache.get(wraw_token)["user_obj"]
token = raw_token
else:
try:
# 对收到的access_token 进行认证
token = self.get_validated_token(raw_token)
# 根据认证后的 access_token 获取用户对象
user = self.get_user(token)
except Exception as e:
# 普通 access_token异常不进行处理 该怎样抛出响应就抛出响应
# 原access_token异常,为刷新接口时,获取 request.data 的 refresh_token
if request.data.get("refresh_token"):
refresh_token = request.data.get("refresh_token")
# 获取当前 refresh_token 的 有效时间
refresh_time_remaining = output_time_remaining(refresh_token)
# refresh_token 在有效期内
if refresh_time_remaining > 0:
# 且不在黑名单中
if not cache.has_key(refresh_token):
decoded_data = jwt_decode(str(refresh_token), config.JWT_SECRET_KEY, verify=False,
algorithms="HS256")
# 获取 用户对象
user = Person_info.objects.get(**{
'person_id': decoded_data["user_id"]})
return user, token
def get_user(self, validated_token):
try:
# 是否被录入黑名单
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
边栏推荐
- HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
- 输入一个表达式(用字符串表示),求这个表达式的值。
- 码蹄集 - MT3149 · AND - 数据不是很强,暴力剪枝就能骗AC
- bat操作ftp上传下载命令
- A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)
- [medical segmentation] u2net
- win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
- 数据治理:元数据管理实施(第四篇)
- 芯片,建立在沙粒上的帝国!
- It's not that you have a bad mind, but that you haven't found the right tool
猜你喜欢

scope 数据导出mat

Dear pie users, I want to confess to you!

Crossing pie · pie pan + Mountain duck = local data management

数据库连接池的简单实现

College community management system based on boot+jsp (with source code download link)

Advanced cross platform application development (II): uni app practice

葫芦儿 APP 使用帮助

Mongodb學習篇:安裝後的入門第一課

为什么用葫芦儿派盘取代U盘?

In depth understanding of condition source code interpretation and analysis of concurrent programming
随机推荐
OpenGL ES: (1) OpenGL ES的由来 (转)
Advanced cross platform application development (II): uni app practice
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
葫芦儿 APP 使用帮助
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
HDU - 1069 Monkey and Banana(DP+LIS)
码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
Daily code 300 lines learning notes day 11
Typeorm framework
穿越派·派盘 + 思源笔记 = 私人笔记本
A little assistant for teenagers' physiological health knowledge based on wechat applet (free source code + project introduction + operation introduction + operation screenshot + Thesis)
输入一个表达式(用字符串表示),求这个表达式的值。
如何添加葫芦儿派盘
Is it safe for a novice to open a securities account?
Some errors encountered in MySQL data migration
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
HDU - 1024 Max Sum Plus Plus(DP)
数据治理:数据治理框架(第一篇)