Django JWT身份验证 - 用户在中间件中是匿名的

9

我在我的项目中使用Django JWT来提升身份验证系统。同时,我还有一个中间件,问题是,在其中用户出于某种原因是匿名的,而在视图中,我能够通过 request.user 访问正确的用户。这个问题让我发疯,因为一段时间以前这个代码完美地运行了!这是JWT的错误还是我做错了什么?

class TimezoneMiddleware(MiddlewareMixin):
         def process_request(self, request):
            # request.user is ANONYMOUS HERE !!!!
            if not request.user.is_anonymous:
                  tzname = UserProfile.objects.get(user = request.user).tz_name
                  if tzname:
                       timezone.activate(pytz.timezone(tzname))

相关的settings.py模块:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
        # Any other renders
    ),

    'DEFAULT_PARSER_CLASSES': (
        'djangorestframework_camel_case.parser.CamelCaseJSONParser',
        # Any other parsers
    ),
}

JWT_AUTH = {
    'JWT_ENCODE_HANDLER':
    'rest_framework_jwt.utils.jwt_encode_handler',

    'JWT_DECODE_HANDLER':
    'rest_framework_jwt.utils.jwt_decode_handler',

    'JWT_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_payload_handler',

    'JWT_PAYLOAD_GET_USER_ID_HANDLER':
    'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',

    'JWT_RESPONSE_PAYLOAD_HANDLER': 'rest_framework_jwt.utils.jwt_response_payload_handler', 
    # 'rest_authentication.views.jwt_response_payload_handler',
    'JWT_SECRET_KEY': SECRET_KEY,
    'JWT_PUBLIC_KEY': None,
    'JWT_PRIVATE_KEY': None,
    'JWT_ALGORITHM': 'HS256',
    'JWT_VERIFY': True,
    'JWT_VERIFY_EXPIRATION': False,
    'JWT_LEEWAY': 0,
    'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),
    'JWT_AUDIENCE': None,
    'JWT_ISSUER': None,
    'JWT_ALLOW_REFRESH': False,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
    'JWT_AUTH_HEADER_PREFIX': 'JWT',
}

我也找到了一些资源,帮助我检索实际用户,但是!我仍然无法设置时区(timezone.activate(pytz.timezone(tzname))似乎被忽略了。

2个回答

9

是的,这个问题是由JWT引起的。您可以查看相关讨论:https://github.com/GetBlimp/django-rest-framework-jwt/issues/45要解决这个问题,您需要创建一个自定义中间件来设置request.user。以下是我在代码中使用的示例:

from django.contrib.auth.middleware import get_user
from django.utils.functional import SimpleLazyObject
from rest_framework_jwt.authentication import JSONWebTokenAuthentication


class JWTAuthenticationMiddleware(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

将此内容包含在中间件中。它应该位于所有使用 request.user 的中间件之上。


1

@Atul Mishra: 谢谢你!我将你的版本更改为最新的drf-jwt包(1.17.2)。看起来当前的github存储库已从this移动到here

from django.contrib.auth.middleware import get_user
from django.utils.functional import SimpleLazyObject
from rest_framework_jwt.authentication import JSONWebTokenAuthentication


class JWTAuthenticationInMiddleware(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):
        # Already authenticated
        user = get_user(request)
        if user.is_authenticated:
            return user

        # Do JTW authentication
        jwt_authentication = JSONWebTokenAuthentication()

        authenticated = jwt_authentication.authenticate(request)
        if authenticated:
            user, jwt = authenticated

        return user

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接