Python 创建新中间件后,无法使用JWT令牌在Django Rest Framework自定义请求中间件中使用用户
我想在Django Rest框架自定义中间件中使用Python 创建新中间件后,无法使用JWT令牌在Django Rest Framework自定义请求中间件中使用用户,python,django,django-rest-framework,jwt,django-middleware,Python,Django,Django Rest Framework,Jwt,Django Middleware,我想在Django Rest框架自定义中间件中使用request.user。 它返回AnnonymousUser,我失败了 我创建了新的自定义中间件,它返回真实用户 from django.contrib.auth.middleware import get_user from django.utils.functional import SimpleLazyObject from rest_framework_jwt.authentication import JSONWebTokenAuth
request.user
。
它返回AnnonymousUser,我失败了
我创建了新的自定义中间件,它返回真实用户
from django.contrib.auth.middleware import get_user
from django.utils.functional import SimpleLazyObject
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
class AuthenticationMiddlewareJWT(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
request.user = SimpleLazyObject(lambda: self.__class__.get_jwt_user(request))
return self.get_response(request)
@staticmethod
def get_jwt_user(request):
user = get_user(request)
if user.is_authenticated:
return user
jwt_authentication = JSONWebTokenAuthentication()
if jwt_authentication.get_jwt_value(request):
user, jwt = jwt_authentication.authenticate(request)
return user
在中间件上面,jwt\u身份验证。获取jwt\u值(请求)
,它总是返回None
如何修复它并在自定义中间件中使用request.user