Django的认证有什么诀窍

2024-09-28 19:32:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读Django的源代码,遇到了一个关于AuthenticationMiddleware的问题。在

如文档所述,认证中间件

Adds the user attribute (a instance of User model) to every incoming HttpRequest

但我不明白在AuthenticationMiddleware.process_request()中是怎么做到的。如下面的代码所示,process_request这里只需将LazyUser()分配给request.__class__,这与User模型无关。而且LazyUser.__get__()看起来很奇怪,让我很困惑。在

class LazyUser(object):
    def __get__(self, request, obj_type=None):
        if not hasattr(request, '_cached_user'):
            from django.contrib.auth import get_user
            request._cached_user = get_user(request)
        return request._cached_user

class AuthenticationMiddleware(object):
    def process_request(self, request):
        assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
        request.__class__.user = LazyUser()
        return None

简单地说,我想知道当AuthenticationMiddleware钩住处理HttpRequest时,幕后究竟发生了什么??在


Tags: todjangogetobjectrequestdefprocessmiddleware
2条回答

LazyUser对象是一个Python descriptor,也就是说,一个可以指定如何通过其父类的实例访问自身的对象。(那是一口)让我看看能不能给你讲讲:

# Having a LazyUser means we don't have to get the actual User object
# for each request before it's actually accessed.
class LazyUser(object):
    # Define the __get__ operation for the descripted object.
    # According to the docs, "descr.__get__(self, obj, type=None)  > value".
    # We don't need the type (obj_type) for anything, so don't mind that.
    def __get__(self, request, obj_type=None):
        # This particular request doesn't have a cached user?
        if not hasattr(request, '_cached_user'):
            # Then let's go get it!
            from django.contrib.auth import get_user
            # And save it to that "hidden" field.
            request._cached_user = get_user(request)
        # Okay, now we have it, so return it.
        return request._cached_user

class AuthenticationMiddleware(object):
    # This is done for every request...
    def process_request(self, request):
        # Sanity checking.
        assert hasattr(request, 'session'), "blah blah blah."
        # Put the descriptor in the class's dictionary. It can thus be
        # accessed by the class's instances with `.user`,  and that'll
        # trigger the above __get__ method, eventually returning an User, 
        # AnonymousUser, or what-have-you.
        # Come to think of it, this probably wouldn't have to be done 
        # every time, but the performance gain of checking whether we already
        # have an User attribute would be negligible, or maybe even negative.
        request.__class__.user = LazyUser()
        # We didn't mess with the request enough to have to return a
        # response, so return None.
        return None

这有帮助吗?:)

懒虫只是一个包装,一个懒惰的包装。get方法是Python的神奇方法之一,当访问die对象时,将调用该方法。因此,只有在实际使用时才确定用户。这是有意义的,因为此操作将触发数据库调用,并且只有在用户确实需要时才应该这样做。在

LazyUser不是分配给请求实例本身,而是分配给reqeust的实例类,这也使它对实例可用。我不知道为什么要这样做。在

相关问题 更多 >