Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建新中间件后,无法使用JWT令牌在Django Rest Framework自定义请求中间件中使用用户_Python_Django_Django Rest Framework_Jwt_Django Middleware - Fatal编程技术网

Python 创建新中间件后,无法使用JWT令牌在Django Rest Framework自定义请求中间件中使用用户

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

我想在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 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