使用ldap配置文件在Django管理中进行身份验证

2024-10-01 17:24:18 发布

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

我正在开发一个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

在管理界面,登录失败。
我的设置.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=组,dc=whitecube
    • cn=超级用户,ou=组,dc=whiteqube
    • cn=员工,ou=团队,dc=怀特库贝
  • ou=用户,dc=白色Qube
    • cn=sonia,ou=用户,dc=whiteqube

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

LDAP Tree
我确信ldap对象的配置是必须的,因为Django debug可以识别用户的组依赖性。

注:当我使用django本地帐户时,我可以登录admin。

我错在哪里了?我还错过了其他属性配置吗?在


Tags: todjango用户authisoudccn
2条回答

我终于成功了! 调试:用户必须属于所有组(活动组、人员组、超级用户组)才能登录管理界面,至少已经创建了一个新的个人组。在

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

谢谢。再见

实际上,我已经解决了LDAP对象的问题。
我添加了一些部件到设置.py并更改了LDAP树的结构(链接到下面的图像)。
现在,如果我尝试使用LDAP用户的信息登录,程序将填充Django Users表中的一行。在检查Django数据库时,我注意到Django管理员无法读取用户密码,但是django_auth_ldap documentation指定它是正常的。在

但是,我仍然无法登录。
我发现的新错误是:

[26/Aug/2014 09:42:15] "GET /admin/ HTTP/1.1" 200 1870
search_s('ou=users,dc=whiteqube', 2, '(uid=%(user)s)') returned 1 objects: cn=marco rossi,ou=users,dc=whiteqube
DEBUG:django_auth_ldap:search_s('ou=users,dc=whiteqube', 2, '(uid=%(user)s)') returned 1 objects: cn=marco rossi,ou=users,dc=whiteqube
cn=marco rossi,ou=users,dc=whiteqube is a member of cn=enabled,ou=groups,dc=whiteqube
DEBUG:django_auth_ldap:cn=marco rossi,ou=users,dc=whiteqube is a member of cn=enabled,ou=groups,dc=whiteqube
cn=marco rossi,ou=users,dc=whiteqube is not a member of cn=disabled,ou=groups,dc=whiteqube
DEBUG:django_auth_ldap:cn=marco rossi,ou=users,dc=whiteqube is not a member of cn=disabled,ou=groups,dc=whiteqube
Populating Django user mrossi
DEBUG:django_auth_ldap:Populating Django user mrossi
cn=marco rossi,ou=users,dc=whiteqube is a member of cn=superuser,ou=groups,dc=whiteqube
DEBUG:django_auth_ldap:cn=marco rossi,ou=users,dc=whiteqube is a member of cn=superuser,ou=groups,dc=whiteqube
cn=marco rossi,ou=users,dc=whiteqube is not a member of cn=staff,ou=groups,dc=whiteqube
DEBUG:django_auth_ldap:cn=marco rossi,ou=users,dc=whiteqube is not a member of cn=staff,ou=groups,dc=whiteqube
cn=marco rossi,ou=users,dc=whiteqube is a member of cn=active,ou=groups,dc=whiteqube
DEBUG:django_auth_ldap:cn=marco rossi,ou=users,dc=whiteqube is a member of cn=active,ou=groups,dc=whiteqube
/home/andrea/PycharmProjects/wq_asja_gateway_v1/env/local/lib/python2.7/site-packages/django_auth_ldap/backend.py:590: DeprecationWarning: The use of AUTH_PROFILE_MODULE to define user profiles has been deprecated.
  profile = self._user.get_profile()

WARNING:py.warnings:/home/andrea/PycharmProjects/wq_asja_gateway_v1/env/local/lib/python2.7/site-packages/django_auth_ldap/backend.py:590: DeprecationWarning: The use of AUTH_PROFILE_MODULE to define user profiles has been deprecated.
  profile = self._user.get_profile()

Django user mrossi does not have a profile to populate
DEBUG:django_auth_ldap:Django user mrossi does not have a profile to populate

我的新产品设置.py配置:

^{pr2}$

LDAP tree …其中:

  • cn=marco rossi(我用来登录的帐户)是一个posixAccount,是cn=superuser和cn=enabled的成员,两者都是posixGroup。在

有什么建议吗?在

相关问题 更多 >

    热门问题