当前位置:网站首页>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
边栏推荐
- Crossing pie · pie pan + Mountain duck = local data management
- Simple implementation of database connection pool
- Unity uses SQLite
- HCM 初学 ( 一 ) - 简介
- Educational administration management system of SSM (free source code)
- excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
- 加密狗资料搜集
- Mongodb学习篇:安装后的入门第一课
- 2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
- 穿越派·派盘 + Mountain Duck = 数据本地管理
猜你喜欢

从MLPerf谈起:如何引领AI加速器的下一波浪潮
![[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc](/img/c8/e3e31ad9ef214d97228cb501dd752f.jpg)
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc

HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10

教务管理系统(免费源码获取)

【QT】qt加减乘除之后,保留小数点后两位

Debug details under pycharm

What is the at instruction set often used in the development of IOT devices?

OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow

boot+jsp的高校社團管理系統(附源碼下載鏈接)

【知识点总结】卡方分布,t分布,F分布
随机推荐
Advanced cross platform application development (II): uni app practice
Is it safe for a novice to open a securities account?
基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)
Send you through the data cloud
win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
rust猜数字游戏
OpenGL ES: (1) OpenGL ES的由来 (转)
从底层结构开始学习FPGA----RAM IP的定制与测试
tese_Time_2h
穿越派·派盘 + 思源笔记 = 私人笔记本
Bat operation FTP upload and download command
Why use huluer pie disk instead of U disk?
HCM 初学 ( 一 ) - 简介
HDU - 1024 Max Sum Plus Plus(DP)
tese_ Time_ 2h
2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
【QT】qt加减乘除之后,保留小数点后两位
Simple implementation of database connection pool
It's not that you have a bad mind, but that you haven't found the right tool