当前位置:网站首页>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
边栏推荐
- 论文学习记录随笔 多标签之GLOCAL
- HCM 初学 ( 四 ) - 时间
- Unity uses SQLite
- Idea start view project port
- Advanced cross platform application development (III): online resource upgrade / hot update with uni app
- College community management system based on boot+jsp (with source code download link)
- Code shoe set - mt3114 · interesting balance - explain it with examples
- 2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
- scope 数据导出mat
- json数据比较器
猜你喜欢

Debug details under pycharm

Ucosiii --- engineering transplantation

Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal

Typeorm framework

mysql 将毫秒数转为时间字符串

Build 2022 上开发者最应关注的七大方向主要技术更新

数据库连接池的简单实现

ssm+mysql二手交易网站(论文+源码获取链接)

C语言初阶——牛客网精选好题

论文学习记录随笔 多标签之GLOCAL
随机推荐
Speed regulation and stroke control based on Ti drv8424 driving stepper motor
论文学习记录随笔 多标签之LSML
Chip, an empire built on sand!
In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
HDU - 1024 Max Sum Plus Plus(DP)
Crossing pie · pie pan + Mountain duck = local data management
C语言初阶——实现扫雷游戏
HCM 初学 ( 二 ) - 信息类型
Common solutions for mobile terminals
2022第八届中国国际“互联网+”大学生创新创业大赛产业命题赛道开启报名!
论文学习记录随笔 多标签之GLOCAL
穿越派与贸大合作,为大学生增添效率
Advanced cross platform application development (III): online resource upgrade / hot update with uni app
【问题思考总结】为什么寄存器清零是在用户态进行的?
Idea start view project port
bat操作ftp上传下载命令
为了保护自己的数据,他奋斗了一天一夜
Code shoe set - mt3114 · interesting balance - explain it with examples
云盘里资料被和谐了,怎么办?
HDU - 1069 Monkey and Banana(DP+LIS)