当前位置:网站首页>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
边栏推荐
- srpingboot security demo
- 2022 年面向初学者的 10 大免费 3D 建模软件
- PLA not pasted on the bed: 6 simple solutions
- 地宮取寶(記憶化深搜)
- Golang panic recover custom exception handling
- MySQL里记录货币
- Factorial divisor (unique decomposition theorem)
- LED lighting used in health lighting
- π disk, turning your computer into a personal private cloud
- 【网络安全工具】USB控制软件有什么用
猜你喜欢

PLA不粘贴在床上:6个简单的解决方案

π disk, turning your computer into a personal private cloud

Pla ne colle pas sur le lit: 6 solutions simples

Top 10 Free 3D modeling software for beginners in 2022

Movable mechanical wall clock

One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is

SystemVerilog学习-10-验证量化和覆盖率

【企业数据安全】升级备份策略 保障企业数据安全

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用

Understanding of C manualresetevent class
随机推荐
Talking from mlperf: how to lead the next wave of AI accelerator
skywalking集成nacos动态配置
蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
Servlet
Pla ne colle pas sur le lit: 6 solutions simples
SystemVerilog学习-09-进程间同步、通信和虚方法
XAF Bo of dev XPO comparison
Arcserver password reset (account cannot be reset)
68 Cesium代码datasource加载czml
Flink实战--多流合并
Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
【ITSM】什么是ITSM,IT部门为什么需要ITSM
【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用
Top 10 Free 3D modeling software for beginners in 2022
Freeswitch dial the extension number
Treasure taking from underground palace (memory based deep search)
Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
Oracle sequence + trigger
让厦门灌口镇田头村变“甜头”村的特色农产品之一是
ABP 学习解决方案中的项目以及依赖关系