我设法使ldap身份验证工作起来,但是用户组不起作用。当用户的用户名、名字、电子邮件..etc被复制到会话中时,布尔值(从用户所属的组获得)不是。
这是我的settings.py :
AUTHENTICATION_BACKENDS = [
'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
]
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, GroupOfUniqueNamesType
AUTH_LDAP_SERVER_URI = "ldap://openldap"
AUTH_LDAP_BIND_DN = "cn=admin,dc=openldap"
AUTH_LDAP_BIND_PASSWORD = "admin"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=django,dc=openldap",
ldap.SCOPE_SUBTREE, "(cn=%(user)s)")
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail",
}
AUTH_LDAP_CACHE_TIMEOUT = 0
AUTH_LDAP_GROUP_CACHE_TIMEOUT = 0
AUTH_LDAP_PROFILE_ATTR_MAP = {"home_directory": "homeDirectory"}
AUTH_LDAP_MIRROR_GROUPS = True
AUTH_LDAP_FIND_GROUP_PERMS = True
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("dc=openldap",
ldap.SCOPE_SUBTREE, "(objectClass=*)"
)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr='cn')
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
'is_active': 'cn=active,ou=groups,dc=openldap',
'is_staff': 'cn=staff,ou=groups,dc=openldap',
'is_superuser': 'cn=superuser,ou=groups,dc=openldap',
}
# # Simple group restrictions
# AUTH_LDAP_REQUIRE_GROUP = 'cn=enabled,ou=groups,dc=openldap',
# AUTH_LDAP_DENY_GROUP = 'cn=disabled,ou=groups,dc=openldap',
### ERROR LOGGING
import logging
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)这是我的ldap方案:

根是活动、工作人员和超级用户的一部分。
user1是active的一部分。
这就是当我从视图对用户进行身份验证时得到的结果:我得到的错误->不是
openldap | 5b444c1f conn=1015 fd=13 ACCEPT from IP=172.23.0.4:47230 (IP=0.0.0.0:389)
openldap | 5b444c1f conn=1015 op=0 BIND dn="cn=admin,dc=openldap" method=128
openldap | 5b444c1f conn=1015 op=0 BIND dn="cn=admin,dc=openldap" mech=SIMPLE ssf=0
openldap | 5b444c1f conn=1015 op=0 RESULT tag=97 err=0 text=
openldap | 5b444c1f conn=1015 op=1 SRCH base="ou=django,dc=openldap" scope=2 deref=0 filter="(cn=root)"
openldap | 5b444c1f <= mdb_equality_candidates: (cn) not indexed
openldap | 5b444c1f conn=1015 op=1 SEARCH RESULT tag=101 err=0 nentries=1 text=
django | search_s('ou=django,dc=openldap', 2, '(cn=%(user)s)') returned 1 objects: cn=root,ou=django,dc=openldap
openldap | 5b444c1f conn=1015 op=2 BIND anonymous mech=implicit ssf=0
openldap | 5b444c1f conn=1015 op=2 BIND dn="cn=root,ou=django,dc=openldap" method=128
openldap | 5b444c1f conn=1015 op=2 BIND dn="cn=root,ou=django,dc=openldap" mech=SIMPLE ssf=0
openldap | 5b444c1f conn=1015 op=2 RESULT tag=97 err=0 text=
django | Populating Django user root
openldap | 5b444c1f conn=1015 op=3 BIND anonymous mech=implicit ssf=0
openldap | 5b444c1f conn=1015 op=3 BIND dn="cn=admin,dc=openldap" method=128
openldap | 5b444c1f conn=1015 op=3 BIND dn="cn=admin,dc=openldap" mech=SIMPLE ssf=0
openldap | 5b444c1f conn=1015 op=3 RESULT tag=97 err=0 text=
openldap | 5b444c1f conn=1015 op=4 CMP dn="cn=active,ou=groups,dc=openldap" attr="member"
openldap | 5b444c1f conn=1015 op=4 RESULT tag=111 err=16 text=
django | cn=root,ou=django,dc=openldap is not a member of cn=active,ou=groups,dc=openldap
openldap | 5b444c1f conn=1015 op=5 CMP dn="cn=staff,ou=groups,dc=openldap" attr="member"
openldap | 5b444c1f conn=1015 op=5 RESULT tag=111 err=16 text=
openldap | 5b444c1f conn=1015 op=6 CMP dn="cn=superuser,ou=groups,dc=openldap" attr="member"
openldap | 5b444c1f conn=1015 op=6 RESULT tag=111 err=16 text=
django | cn=root,ou=django,dc=openldap is not a member of cn=staff,ou=groups,dc=openldap
django | cn=root,ou=django,dc=openldap is not a member of cn=superuser,ou=groups,dc=openldap
openldap | 5b444c1f conn=1015 op=7 SRCH base="dc=openldap" scope=2 deref=0 filter="(&(objectClass=*)(member=cn=root,ou=django,dc=openldap))"
openldap | 5b444c1f <= mdb_equality_candidates: (member) not indexed
openldap | 5b444c1f conn=1015 op=7 SEARCH RESULT tag=101 err=0 nentries=0 text=
django | search_s('dc=openldap', 2, '(&(objectClass=*)(member=cn=root,ou=django,dc=openldap))') returned 0 objects:PS:用户是独立的,没有问题,但是当我打印user.is_staff、user.is_active和user.is_superuser时,它们都会得到False。
发布于 2020-08-21 21:49:47
我知道我已经晚了几年才回复,但是当我试图解决我自己的问题时,我无意中发现了你的问题,让django-auth-ldap和我的AD服务器一起工作。
从您的日志中,我可以看到一些这样的消息:
django | cn=root,ou=django,dc=openldap is not a member of cn=superuser,ou=groups,dc=openldap对我来说,解决问题的是这里的以下引语
如果筛选器太特定,则会忽略中间的组,从而导致不完整的组列表。 不要让你的过滤器太具体。不管怎么说,这并不重要,因为只有当用户属于他们,甚至间接的情况下,这些组才会被返回。
虽然特别不适用于您的情况,也可能与您的情况相反:我认为这是问题的症结所在。django-auth-ldap找不到你们的小组。我想你是从根开始的,也就是DC。尝试将您的AUTH_LDAP_GROUP_SEARCH更改为更具体一些,如下面所示,看看这是否有帮助:
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
"OU=groups,DC=openldap",
ldap.SCOPE_SUBTREE,
"(objectClass=group)",
)我根本不是LDAP / AD方面的专家,所以YMMV。
https://stackoverflow.com/questions/51258224
复制相似问题