在Django项目中使用ldap验证用户

时间:2018-02-08 17:05:52

标签: python django ldap

我正在尝试针对我的ldap服务器对用户进行身份验证。

我已经成功运行了pip install python-ldap和django-auth-ldap ..

我正在运行python 2.7 ..

每当我尝试使用我的超级用户帐户登录我的Django管理员时,我都会收到以下错误:

AttributeError at /admin/login/
'module' object has no attribute 'LDAPError'
Request Method: POST
Request URL:    http://localhost:8000/admin/login/?next=/admin/
Django Version: 1.11.9
Exception Type: AttributeError
Exception Value:    
'module' object has no attribute 'LDAPError'
Exception Location: C:\Python27\lib\site-packages\django_auth_ldap\backend.py in authenticate, line 384
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.13
Python Path:    
['C:\\Users\\donallane\\Desktop\\My_Django_Stuff\\ldap-login\\learning_users',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\python_ldap-2.4.10-py2.7-win32.egg']
Server time:    Thu, 8 Feb 2018 16:59:10 +0000

settings.py中的ldap配置:

####### ---------------  LDAP Configuration ------------ #######
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType

# AUTH_LDAP_START_TLS = True
# AUTH_LDAP_GLOBAL_OPTIONS = {
#     #ldap.OPT_X_TLS_REQUIRE_CERT: False,
#     #ldap.OPT_REFERRALS: False,
# }
#
# AUTH_LDAP_SERVER_URI = "ldaps://ldap.ecf.orca.ie"
# AUTH_LDAP_BIND_DN = 'cn=myapp,ou=apps,o=company'
# AUTH_LDAP_BIND_PASSWORD = 'myapp_password'
# AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=people,o=company", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
#
# AUTH_LDAP_ALWAYS_UPDATE_USER = False
# #
# AUTH_LDAP_USER_ATTR_MAP = {
#     "first_name": "givenName",
#     "last_name": "sn",
#     "email": "mail"
# }

# import ldap
#
# AUTH_LDAP_CONNECTION_OPTIONS = {
#     ldap.OPT_REFERRALS: 0
# }

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

auth_connection.py:

import ldap
# try this code and catch the ldap error
try:
    l = ldap.initialize("ldap://ldap.ecf.orca.ie")
    out = l.simple_bind_s("uid=username,ou=people,o=company", "password")

    print out
    exit()
# any errors will throw an LDAP error exception
# or related exception so you can ignore the result
except ldap.LDAPError as e:
    print e

pip冻结的输出:

Django==1.11.9
django-auth-ldap==1.3.0
django-filter==1.1.0
djangorestframework==3.7.7
djangorestframework-jwt==1.11.0
httplib2==0.10.3
ldap==1.0.2
ldap3==2.4.1
Markdown==2.6.11
oauth2==1.9.0.post1
psycopg2==2.7.3.2
pyasn1==0.4.2
PyJWT==1.5.3
python-ldap==2.4.10
python-oauth2==1.0.1
pytz==2017.3

0 个答案:

没有答案