问题描述
我有一个用 Django 编写的应用程序,我必须扩展它并包含一些其他解决方案作为应用程序";在这个应用程序中.
I have an application written in Django and I have to extend it and include some other solution as an "app" in this application.
例如,我要集成的应用名为my_new_app";现在有一个为主应用编写的后端认证,我无法使用它.
For example, my app to be integrated is named "my_new_app"Now there is a backend authentication written for the main application and I cannot use it.
我有一个 MySQL 数据库可供查询,主应用程序主要使用 Cassandra 和 Redis.
I have a MySQL DB to query from and the main app uses Cassandra and Redis mostly.
有什么办法可以为新应用程序my_new_app"使用单独的身份验证后端?并在同一个域中运行?
Is there any way I can use a separate authentication backend for the new app "my_new_app" and run both in the same domain?
推荐答案
您可以有多个身份验证后端.只需在 Django 项目的 settings.py
中设置 AUTHENTICATION_BACKENDS
即可列出要使用的后端实现.例如,我经常使用 OpenID 身份验证和标准 Django 身份验证的组合,例如在我的 settings.py
中:
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',
)
在此示例中,Django 将首先尝试使用 django.contrib.auth.backends.ModelBackend
进行身份验证,这是 Django 的默认后端.如果失败,那么它移动到下一个后端,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
.
请注意,您的自定义后端必须位于 Django 可见的路径上.在此示例中,我必须将 django_openid_auth
添加到 INSTALLED_APPS
,否则 Django 将无法导入它并将其用作后端.
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.
还看了相关文档,写的很好,通俗易懂:https://docs.djangoproject.com/en/dev/topics/auth/customizing/
Also read the relevant documentation, it's very nicely written, easy to understand:https://docs.djangoproject.com/en/dev/topics/auth/customizing/
这篇关于一个项目的 Django 多重身份验证后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!