当前位置: 首页>>代码示例>>Python>>正文


Python middleware.RemoteUserMiddleware方法代码示例

本文整理汇总了Python中django.contrib.auth.middleware.RemoteUserMiddleware方法的典型用法代码示例。如果您正苦于以下问题:Python middleware.RemoteUserMiddleware方法的具体用法?Python middleware.RemoteUserMiddleware怎么用?Python middleware.RemoteUserMiddleware使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.contrib.auth.middleware的用法示例。


在下文中一共展示了middleware.RemoteUserMiddleware方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: process_request

# 需要导入模块: from django.contrib.auth import middleware [as 别名]
# 或者: from django.contrib.auth.middleware import RemoteUserMiddleware [as 别名]
def process_request(self, request):
        # Overwrite process_request from auth.middleware because it force
        # user logout when REMOTE_USER header is not present which can
        # cause problem while deploying with Kerberos authentication when
        # we need to enable both anonymous access and kerberos login.

        # AuthenticationMiddleware is required so that request.user exists.
        if not hasattr(request, 'user'):
            raise ImproperlyConfigured(
                "The Django remote user auth middleware requires the"
                " authentication middleware to be installed.  Edit your"
                " MIDDLEWARE setting to insert"
                " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
                " before the RemoteUserMiddleware class.")

        if settings.DEBUG and getattr(settings, "DEBUG_USER", None):
            request.META[self.header] = settings.DEBUG_USER

        try:
            username = request.META[self.header]
        except KeyError:
            # When the page which requires kerberos login was redirected from
            # kerberos login entrance, 'REMOTE_USER' header is lost in request
            # meta, thus the RemoteUserMiddleware will make it falling into
            # redirect loop.
            return

        # If the user is already authenticated and that user is the user we are
        # getting passed in the headers, then the correct user is already
        # persisted in the session and we don't need to continue.
        if request.user.is_authenticated:
            if request.user.get_username() == self.clean_username(username, request):
                return
        # We are seeing this user for the first time in this session, attempt
        # to authenticate the user.
        user = auth.authenticate(remote_user=username, request=request)
        if user:
            # User is valid.  Set request.user and persist user in the session
            # by logging the user in.
            request.user = user
            request.session['auth_backend'] = user.backend
            backend = load_backend(user.backend)
            if getattr(backend, 'save_login', True):
                auth.login(request, user) 
开发者ID:product-definition-center,项目名称:product-definition-center,代码行数:46,代码来源:middleware.py


注:本文中的django.contrib.auth.middleware.RemoteUserMiddleware方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。