【问题标题】:Help with Django RemoteUserMiddleware RemoteUserBackend with fresh database使用新数据库帮助 Django RemoteUserMiddleware RemoteUserBackend
【发布时间】:2011-08-18 04:10:06
【问题描述】:

Django 1.3 版本包括RemoteUserMiddlewareRemoteUserBackend 允许 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 输出显示,设置实际上按请求的顺序具有 AuthenticationMiddlewareRemoteUserMiddleware

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


【解决方案1】:

我遇到了同样的问题,经过一点调试后发现我忘记为 auth 和其他应用程序创建 mysql(我认为 sqllite 的情况相同)表。因此,如果您不这样做,则需要运行 python manage.py syncdb 或以其他方式创建它们

【讨论】:

    【解决方案2】:

    这是一个丑陋的解决方法。

    我创建了一个单元测试用例,它在股票开发环境中运行良好。基于此,我在Apache服务器上进入了上面提到的RemoteMilddleware.process_request()函数,并禁用了user属性的测试。然后它工作正常......

    我不会选择此作为答案,但我认为我应该为其他被阻止的人记录此解决方法。我会考虑提交错误报告。

    有人知道正确的解决方案或如何在 Apache2 中攻击它吗?所有的回溯和变量信息都表明 Apache 正确设置了 REMOTE_USER 但 Django1.3 没有正确使用它进行身份验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2021-12-03
      相关资源
      最近更新 更多