【问题标题】:Django RemoteUserBackendDjango 远程用户后端
【发布时间】:2014-02-11 20:23:09
【问题描述】:

我正在尝试在 Django 1.5 中使用多个身份验证后端。

我想将RemoteUserBackend 与自定义header 和标准ModelBackend 一起使用

似乎我可以完成其中一项工作,但不能同时完成。如果我尝试使用ModelBackend 登录,我会收到此错误:

"'CustomHeaderMiddleware' object has no attribute 'authenticate'" 

settings.py:

MIDDLEWARE_CLASSES = (
    ...
    'myapp.backends.custom_auth.CustomHeaderMiddleware',
    ...
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django.contrib.auth.backends.RemoteUserBackend',
    'myapp.backends.custom_auth.CustomHeaderMiddleware',
)

custom_auth.py:

from django.contrib.auth.middleware import RemoteUserMiddleware

class CustomHeaderMiddleware(RemoteUserMiddleware):
    header = "CUSTOM_USERID"

我不确定我错过了什么。如果我设置了“CUSTOM_USERID”,它可以工作,但我不能使用标准登录。

我错过了什么?

【问题讨论】:

  • 为什么要在身份验证后端使用中间件?
  • 谢谢@karthikr!就是这样。

标签: django custom-authentication remoteusermiddleware


【解决方案1】:

AUTHENTICATION_BACKENDS 中删除'myapp.backends.custom_auth.CustomHeaderMiddleware'

还要确保'django.contrib.auth.middleware.AuthenticationMiddleware',在'myapp.backends.custom_auth.CustomHeaderMiddleware' 之前MIDDLEWARE_CLASSES

例子:

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'myapp.backends.custom_auth.CustomHeaderMiddleware',
    ...
)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 2021-06-15
    相关资源
    最近更新 更多