You can have multiple authentication backends. Just set the AUTHENTICATION_BACKENDS
in settings.py
of your Django project to list the backend implementations you want to use. For example I often use a combination of OpenID authentication and the standard Django authentication, like this in my settings.py
:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'django_openid_auth.auth.OpenIDBackend',
)
In this example Django will first try to authenticate using django.contrib.auth.backends.ModelBackend
, which is the default backend of Django. If that fails, then it moves on to the next backend, django_openid_auth.auth.OpenIDBackend
.
Note that your custom backends must be at a path visible by Django. In this example I have to add django_openid_auth
to INSTALLED_APPS
, otherwise Django won't be able to import it and use it as a backend.
Also read the relevant documentation, it's very nicely written, easy to understand:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…