7

我想使用 django.contrib.auth.middleware.RemoteUserMiddleware 进行身份验证,如下所述:

https://docs.djangoproject.com/en/dev/howto/auth-remote-user/

问题是,如何在没有 Apache 的开发环境中进行测试?即我可以在本地设置中以某种方式设置 REMOTE_USER 吗?

编辑(添加设置)

AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.RemoteUserBackend',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.RemoteUserMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    ...
)

然后我在我的 local_Settings 中有这个:

os.environ['REMOTE_USER'] = "mmatyas"

我还尝试了“HTTP_REMOTE_USER”变体。谢谢!

4

3 回答 3

2

该变量必须在您启动开发服务器的环境中设置。通常只是:

REMOTE_USER=myuser ./manage.py runserver
于 2017-02-15T00:35:42.797 回答
1

您可以编写一些 WSGI 中间件并将其设置在那里,然后确保 wsgi 中间件将您的 django 应用程序包装在 wsgi 堆栈中。有很多关于编写 wsgi 中间件的教程,这将是一小段代码。轻松-peasy-lemon-squeezy!

于 2015-11-30T23:08:01.327 回答
0

In your dev environment, you can set an environment variable in the same command prompt you use to start up your dev server.

Something like export REMOTE_USER="duncan" if on a Unixy machine.

You can also do this by editing your manage.py and setting os.environ['REMOTE_USER'] = "duncan"

于 2013-03-21T17:26:33.357 回答