当前位置:网站首页>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
边栏推荐
- 喊我们大学生个人云服务特供商
- Code shoe set - mt3114 · interesting balance - explain it with examples
- 运行时候的导包搜索路径虽然pycharm中标红但不影响程序的执行
- Chip, an empire built on sand!
- mysql 将毫秒数转为时间字符串
- 基于TI DRV8424驱动步进电机实现调速和行程控制
- Advanced cross platform application development (III): online resource upgrade / hot update with uni app
- 4GB大文件,如何实时远程传输和共享?
- Through cooperation with the University of international trade, we can increase efficiency for college students
- [excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
猜你喜欢

el-table 动态表头渲染 固定第一列 高度问题

导数的左右极限和左右导数的辨析

Advanced cross platform application development (III): online resource upgrade / hot update with uni app

Deeply understand the underlying implementation principle of countdownlatch in concurrent programming

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

扩展点系列之SmartInstantiationAwareBeanPostProcessor确定执行哪一个构造方法 - 第432篇

如何添加葫芦儿派盘

论文学习记录随笔 多标签之GLOCAL

Educational administration management system (free source code)

2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
随机推荐
OneFlow源码解析:算子签名的自动推断
HDU - 1024 Max Sum Plus Plus(DP)
教务管理系统(免费源码获取)
多表操作-外键级联操作
穿越派·派盘 + Mountain Duck = 数据本地管理
论文学习记录随笔 多标签之LIFT
为了保护自己的数据,他奋斗了一天一夜
Speed regulation and stroke control based on Ti drv8424 driving stepper motor
芯片,建立在沙粒上的帝国!
Code shoe set - mt3114 · interesting balance - explain it with examples
What things you didn't understand when you were a child and didn't understand until you grew up?
Leetcode top 100 question 2 Add two numbers
Mongodb學習篇:安裝後的入門第一課
Chip, an empire built on sand!
如何添加葫芦儿派盘
Is it safe for a novice to open a securities account?
First defined here occurs during QT compilation. Causes and Solutions
tese_ Time_ 2h
激活函数简述
Build 2022 上开发者最应关注的七大方向主要技术更新