【发布时间】:2011-08-18 04:10:06
【问题描述】:
Django 1.3 版本包括RemoteUserMiddleware 和
RemoteUserBackend 允许 Apache 进行身份验证的类。
见http://docs.djangoproject.com/en/dev/howto/auth-remote-user/
我有一个 initial_data.json,它在执行 syncdb 时创建一个超级用户。转储数据证实了这一点。
但我发现新创建的数据库似乎无法正确登录。我得到一个 ImproperlyConfigured 异常,上面写着: Django 远程用户身份验证中间件需要安装身份验证中间件。
Edit your MIDDLEWARE_CLASSES setting to insert django.contrib.auth.middleware.AuthenticationMiddleware' before the RemoteUserMiddleware class.
回溯指向RemoteMilddleware.process_request():
def process_request(self, request):
# AuthenticationMiddleware is required so that request.user exists.
if not hasattr(request, 'user'):
raise ImproperlyConfigured(...
Apache 的 DEBUG 输出显示,设置实际上按请求的顺序具有 AuthenticationMiddleware 和 RemoteUserMiddleware:
MIDDLEWARE_CLASSES
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.RemoteUserMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
但是request.user属性没有设置,产生异常。
如果我仔细查看 django.contrib.auth.backends 和中间件的源代码,
我发现AuthenticationMiddleware 正在为LazyUser 注册
请求类。但是RemoteUserBackend 好像没有
调用的 authenticate() 方法是在用户表中查找 remote_user 的地方。
为了创建 request.user,我应该做些什么来调用 authenticate()?
我可以根据需要提供更多信息。顺便说一下,这是使用 SSL。这是否有一些我没有预料到的互动?
我应该提到我正在使用 Apache2.2.14 和 mod_wsgi。
【问题讨论】:
-
这个 django sn-p 合适吗? djangosnippets.org/snippets/1547
-
我在使用远程 mysql 服务器时遇到了同样的问题,但使用本地服务器却可以正常工作
-
这是使用本地 sqlite3 数据库。
标签: python django django-authentication