Django LDAP无法绑定成功的连接

2024-06-24 12:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Django项目上建立LDAP身份验证

我对所有这些事情都非常陌生,找不到任何文档或有用的视频,也找不到任何与此相关的答案

在my settings.py中进行以下更改后,我尝试登录到Django管理面板,它显示我提供的凭据无效

但是,我仍然可以通过我的mariadb凭据登录。这是根/根

注意:为了隐藏敏感信息,我将所有敏感信息添加到% % 当我提供LDAP凭据时,控制台会抛出以下错误:

result(5) raised OPERATIONS_ERROR({'msgtype': 101, 'msgid': 5, 'result': 1, 'desc': 'Operations error', 'ctrls': [], 'info': '000004DC: LdapErr: DSID-0C09075A, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1'},)
search_s('OU=%ou%, DC=%dc% , DC=%dc%', 2, '(sAMAccountName=%(user)s)') returned 0 objects:
Authentication failed for %myusername%@%domain%.com: failed to map the username to a DN.

我的管理员提供了以下LDAP配置设置。这存储在一个文件中,对于所有LDAP配置,它们都指向该文件:

   AuthType Basic
   AuthBasicProvider ldap
   AuthzLDAPAuthoritative off
   AuthLDAPURL "ldap://%domain.abc.lan%/OU=%ou%,OU=%ou%,OU=%ou%,DC=%dc%,DC=%dc%?sAMAccountName?sub"
   AuthLDAPBindDN "CN=%cn% ,OU=%ou%,OU=%ou%,OU=%ou%,OU=%ou%,DC=%dc%,DC=%dc%"
   AuthLDAPBindPassword "%password%"
   AuthLDAPGroupAttribute  %attribute%
   AuthLDAPGroupAttributeIsDN off

现在,我对如何利用这些数据感到非常困惑

这就是my settings.py的外观: 我提供了AuthLDAPBindPassword Auth_LDAP_BIND_PASSWORD,因为其中一个是由我的组织提供的,另一个是由LDAP提供的,我非常困惑可以使用什么,与绑定DN类似

from pathlib import Path
import ldap
from django_auth_ldap.config import LDAPSearch, LDAPSearchUnion, GroupOfNamesType
import os
from ldap import OPT_REFERRALS, SCOPE_SUBTREE
SITE_ID =1


AUTHENTICATION_BACKENDS = (
    "django_auth_ldap.backend.LDAPBackend",
    "django.contrib.auth.backends.ModelBackend",
)

AUTH_LDAP_START_TLS = False

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {"console": {"class": "logging.StreamHandler"}},
"loggers": {"django_auth_ldap": {"level": "DEBUG", "handlers": ["console"]}},
}

AuthLDAPBindDN = "CN=Ldap Account,OU=%ou%,OU=%ou%,OU=%ou%,OU=%ou%,DC=%dc%,DC=%dc%"
AUTH_LDAP_BIND_DN = "CN=Ldap Account,OU=%ou%,OU=%ou%,OU=%ou%,OU=%ou%,DC=%dc%,DC=%dc%"
Auth_LDAP_BIND_PASSWORD = "%password%"
AuthLDAPBindPassword = "%password%"

AUTH_LDAP_USER_ATTR_MAP = {
"username": "sAMAccountName",
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
}   

AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(

LDAPSearch("OU=%ou%, DC=%dc%, DC=%dc%", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),
LDAPSearch("OU=%ou%, DC=%dc%, DC=%dc%", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),
LDAPSearch("OU=%ou%, DC=%dc%, DC=%dc%", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),
LDAPSearch("OU=%ou%, DC=%dc%, DC=%dc%", ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)"),

)

AUTH_LDAP_CONNECTION_OPTIONS = {OPT_REFERRALS: 0}
AUTH_LDAP_SERVER_URI = "ldap://%url%:389"

我真的很高兴把这件事整理好,因为我在过去24小时里一直在努力完成这件事,我不知道是什么阻止了它建立连接


Tags: todjangoimportauthoudcldapscope