shi*_*ppa 6 django ldap django-authentication django-auth-ldap
我的设置:Django-3.0、Python-3.8、django_auth_ldap
我的组织中有 LDAP 服务器(Active Directory 服务器)。我正在构建一个 Django 应用程序,它为所有用户提供一些操作。
我知道 Django 具有内置的用户身份验证机制,但是如果用户存在于用户模型数据库中,它会进行身份验证。
但我的要求是。
所有用户条目都在 LDAP 服务器(Active Directory)中。使用正确的用户凭据 LDAP 服务器对我进行身份验证。
我在 Django 'accounts'应用程序中创建了一个登录页面,
1. 每当我从登录页面输入用户名和密码时,它应该使用我的组织 LDAP 服务器进行身份验证。
2. 登录后,我必须为登录用户保持 5 分钟的活动时间。(Django 身份验证会话)
我看到django_auth_ldap包为我的目的提供了一些见解。
我在settings.py 中有这些内容。
import ldap
##Ldap settings
AUTH_LDAP_SERVER_URI = "ldap://myldapserver.com"
AUTH_LDAP_CONNECTION_OPTIONS = {ldap.OPT_REFERRALS : 0}
AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s, OU=USERS,dc=myldapserver, dc=com"
AUTH_LDAP_START_TLS = True
#Register authentication backend
AUTHENTICATION_BACKENDS = [
"django_auth_ldap.backend.LDAPBackend",
]
Run Code Online (Sandbox Code Playgroud)
在views.py 中调用身份验证。
from django_auth_ldap.backend import LDAPBackend
def accounts_login(request):
username = ""
password = ""
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
auth = LDAPBackend()
user = auth.authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect("/")
else:
error = "Authentication Failed"
return render(request, "accounts/login.html", 'error':error)
return render(request, "accounts/login.html")
Run Code Online (Sandbox Code Playgroud)
但是使用上述方法总是无法通过 LDAP 服务器进行身份验证。
如果我使用普通的 python simple_bind_s() 调用,身份验证在同一个 LDAP 服务器上工作正常。
import ldap
def ldap_auth(username, password):
conn = ldap.initialize(myproj.settings.LDAP_AUTH_URI)
try:
ldap.set_option(ldap.OPT_REFERRALS, 0)
#ldap.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
conn.simple_bind_s(username, password)
except ldap.LDAPError as e:
return f'failed to authenticate'
conn.unbind_s()
return "Success"
Run Code Online (Sandbox Code Playgroud)
有人可以建议我按照我的要求进行 LDAPBackend 身份验证吗?
注意:我没有 LDAP 服务器的管理员权限。
这就是我使用ldap3而没有django_auth_ldap包的方式。
1 - 在your_app/backends.py以下位置创建自定义 AuthenticationBackend :
import logging
from ldap3 import Server, Connection
from ldap3.core.exceptions import LDAPBindError
from django.conf import settings
from django.contrib.auth import get_user_model
logger = logging.getLogger(__name__)
UserModel = get_user_model()
class LDAPBackend:
def authenticate(self, request, username=None, password=None, **kwargs):
# set username to lowercase for consistency
username = username.lower()
# get the bind client to resolve DN
logger.info('authenticating %s' % username)
# set your server
server = Server(settings.LDAP_HOST, get_info=ALL)
try:
conn = Connection(server, f"{username}@{settings.LDAP_DOMAIN}", password=password, auto_bind=True)
except LDAPBindError as e:
logger.info('LDAP authentication failed')
logger.info(e)
return None
user = UserModel.objects.update_or_create(username=username)
return user
def get_user(self, user_id):
try:
return UserModel._default_manager.get(pk=user_id)
except UserModel.DoesNotExist:
return None
Run Code Online (Sandbox Code Playgroud)
2 - 声明LDAPBackend为您的身份验证后端settings.py
AUTHENTICATION_BACKENDS = [
'your_app.backends.LDAPBackend',
'django.contrib.auth.backends.ModelBackend'
]
Run Code Online (Sandbox Code Playgroud)
这允许您使用 django 的本机功能进行身份验证,并且它与管理员一起工作。
要让会话仅工作 5 分钟,请将此设置添加到您的settings.py文件中:
SESSION_COOKIE_AGE = 5 * 60
Run Code Online (Sandbox Code Playgroud)
让我知道它是否适合您。
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |