python – 使用ldap配置文件在django admin中进行身份验证

我正在开发一个Django应用程序,它需要支持LDAP身份验证直接进入默认管理页面.
我已经集成了django-auth-ldap并跟随documentation直到我能理解它.
我已经使用OpenLDAP和php图形界面配置了本地LDAP服务器(我也可以使用ldif文件配置).当我尝试登录管理页面时,Django会在其中找到本地服务器及其用户对象,并且还可以识别用户所属的组.尽管如此,我无法登录.我发现的错误:

[21/Aug/2014 11:06:53] “GET /admin/ HTTP/1.1” 200 1870
search_s(‘ou=users,dc=whiteqube’, 2, ‘(cn=%(user)s)’) returned 1 objects: cn=sonia,ou=users,dc=whiteqube

DEBUG:django_auth_ldap:search_s(‘ou=users,dc=whiteqube’, 2, ‘(cn=%(user)s)’) returned 1 objects: cn=sonia,ou=users,dc=whiteqube

Authentication failed for sonia

DEBUG:django_auth_ldap:Authentication failed for sonia

[21/Aug/2014 11:06:56] “POST /admin/ HTTP/1.1” 200 2046

在Admin界面中,无法登录.
我的settings.py:

# - - - - LDAP CONFIGURATION - - - - #
#
# Importing ldap libraries and applications
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, PosixGroupType

# ...connecting to ldap server (local environment uses IP)
AUTH_LDAP_SERVER_URI = "ldap://10.0.2.15"

# ...account to enter into ldap server (anonymous is not always allowed)
#AUTH_LDAP_BIND_DN = "cn=admin,dc=whiteqube"
#AUTH_LDAP_BIND_PASSWORD = "root"

# ...path where to start to search groups
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=groups,dc=whiteqube",
                                    ldap.SCOPE_SUBTREE, # allow searching from current node to all nodes below
                                    "(objectClass=posixGroup)" # type of object
)
AUTH_LDAP_GROUP_TYPE = PosixGroupType() # a posixGroup is identified by the keyword "cn" into ldap server

# ...associations between ldap and django groups
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
    "is_active": "cn=active,ou=groups,dc=whiteqube",
    "is_staff": "cn=staff,ou=groups,dc=whiteqube",
    "is_superuser": "cn=superuser,ou=groups,dc=whiteqube"
}
AUTH_LDAP_PROFILE_FLAGS_BY_GROUPS = {
    "is_awesome": ["cn=awesome,ou=groups,dc=whiteqube"]
}


# ...node where to start to search users
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=whiteqube",
                                   ldap.SCOPE_SUBTREE, # allow searching from current node to all nodes below
                                   "(cn=%(user)s)"
                                   #"(objectClass=posixAccount)"
                                   #"(objectClass=inetOrgPerson)"
)
# Keep ModelBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# Enable debug for ldap server connection
logger = logging.getLogger('django_auth_ldap')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
# - - - - END  LDAP CONFIGURATION - - - - #

我的LDAP充满了这些对象:

> ou = groups,dc = whitecube
>
> cn =超级用户,ou = groups,dc = whiteqube

>
> cn = staff,ou = groups,dc = whiteqube

> ou = users,dc = whiteqube
>
> cn = sonia,ou = users,dc = whiteqube

其中“groups”和“users”是OrganizationalUnit,“staff”和“superuser”是posixGroup,“sonia”是posixAccount.
查看图片

我确定ldap对象被配置为必须,因为Django调试识别用户的组依赖.

Ps:当我使用django本地帐户时,我可以登录管理员.

我错在哪里?我错过了任何进一步的属性配置吗?

解决方法:

我终于搞定了!
调试:用户必须属于所有组(活动,工作人员,超级用户)才能登录管理界面,至少已创建新的个人组.

在我的上一篇文章中,settings.py和LDAP树的配置是正确的,因此您可以保留有关如何创建LDAP并在django应用程序中实现的信息.请记住:如果您使用默认组,请在所有组中添加用户以允许管理员登录.

谢谢.再见

上一篇:java-将and​​roid连接到openLDAP服务器


下一篇:无法通过pip安装python-ldap