[英]Django custom auth backend doesn't seem to be being called?
I'm using Django 1.8.4 on Python 3, and attempting to create an auth backend which validates a cookie from a legacy ColdFusion web site and create / log the Django user in after checking the value in a database. 我在Python 3上使用Django 1.8.4,并尝试创建一个auth后端,该后端验证来自旧ColdFusion网站的cookie,并在检查数据库中的值后创建/登录Django用户。 In settings, I am including the backend: 在设置中,我包括后端:
AUTHENTICATION_BACKENDS = (
'site_classroom.cf_auth_backend.ColdFusionBackend',
)
And the code for the backend itself; 还有后端本身的代码; SiteCFUser is a model against the SQL Server database user model which contains the active cookie token value: SiteCFUser是针对包含活动cookie令牌值的SQL Server数据库用户模型的模型:
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from users.models import SiteCFUser
class ColdFusionBackend(ModelBackend):
"""
Authenticates and logs in a Django user if they have a valid ColdFusion created cookie.
ColdFusion sets a cookie called "site_web_auth"
Example cookie: authenticated@site+username+domain+8E375588B1AAA9A13BE03E401A02BC46
We verify this cookie in the MS SQL database 'site', table site_users, column user_last_cookie_token
"""
def authenticate(self, request):
User = get_user_model()
print('Hello!')
token=request.COOKIES.get('site_web_auth', None)
print('Token: ' + token)
cookie_bites = token.split('+')
if cookie_bites[0] != "authenticated@site":
# Reality check: not a valid site auth cookie
return None
username = cookie_bites[1]
cf_token = cookie_bites[3]
try:
site_user = SiteCFUser.objects.using('mssqlsite').filter(cf_username=username)
except:
# No user found; redirect to login page
return None
if site_user[0].cftoken == cf_token:
try:
# Does the user exist in Django?
user = User.objects.get(username=username)
except:
# User does not exist, has a valid cookie, create the User.
user = User(username=username)
user.first_name = site_user[0].cf_first_name
user.last_name = site_user[0].cf_last_name
user.email = site_user[0].cf_email
user.save()
else:
return None
def get_user(self, user_id):
User = get_user_model()
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
The problem is, the backend doesn't seem to be called when hitting a URL with a view with @login_required
, or even trying to log in through a form with username and password. 问题是,当使用@login_required
视图@login_required
URL时,甚至试图通过使用用户名和密码的表单登录时,似乎都没有调用后端。 If I force an error by changing the name of the class in settings, or change the name of the class in cf_auth_backend.py, I do get an error. 如果我通过更改设置中的类名来强制执行错误,或者更改了cf_auth_backend.py中的类名,则会收到错误消息。 However, none of the print statements show up in the console. 但是,控制台中不会显示任何打印语句。 I'm clearly missing something here: any idea what I'm not doing right? 我显然在这里错过了一些东西:知道我在做什么不对吗?
While the accepted answer might have helped the OP, it's not a general answer to the question's title. 尽管公认的答案可能对操作员有所帮助,但这并不是对该问题标题的一般性回答。
Authentication back ends do work simply by listing them in AUTHENTICATION_BACKENDS
. 验证后端都在上市只是他们工作AUTHENTICATION_BACKENDS
。 But they may appear to be ignored for various reasons, eg: 但是由于各种原因,它们似乎被忽略了,例如:
urls.py needs to point to something like django.contrib.auth.views.login
urls.py需要指向类似django.contrib.auth.views.login
东西
url(r'^accounts/login/$', django.contrib.auth.views.login)
if it's pointing to some other authentication app. 如果它指向其他身份验证应用程序。 AUTHENTICATION_BACKENDS
may not work. AUTHENTICATION_BACKENDS
可能不起作用。
authenticate()
method must accept a password
keyword, either through password=None
or **kwargs
. authenticate()
方法必须通过password=None
或**kwargs
接受password
关键字。 Probably true for username
too. username
也可能如此。 It won't be called if it doesn't accept that keyword argument. 如果不接受该关键字参数,则不会调用它。 Authentication backends doesn't work that way. 身份验证后端无法正常工作。 They won't be called on each request or on requests where authentication is required. 不会在每个请求或需要身份验证的请求上调用它们。
If you want to log in user based on some cookie, you should call authentication in middleware. 如果要基于某些cookie登录用户,则应在中间件中调用身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.