本文整理匯總了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)