咱们先要拿到dc的数据,以及链接ldap的密码,还有搜索的字段(search_filter), 通常来讲search_filter 这个是从负责ldap运维的同事获取的。attributes 这个是获取哪些字段的数据,犹如mysql 语句的select xx,xxx , 若是吧attributes设置为ALL_ATTRIBUTES,那么就是获取全部字段数据。python
search_base='ou=xx,dc=xxx,dc=com' # ldap运维同事提供 ldaphost = ("ldap://192.168.2.1:389") password="11111" # 这个是管理员的密码 search_filter="(objectclass=*)" from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool,ALL_ATTRIBUTES s = Server(ldaphost) conn = Connection(s,password=password) conn.open() conn.bind() True # 执行完bind方法后,返回true说明正确,若是是false的话,须要检查你Connection参数是否正确 res = conn.search(search_base=search_base,search_filter=search_filter,search_scope=SUBTREE,attributes = ALL_ATTRIBUTES) # 若是res不是false说明search方法执行成功啦。不然检查传入的参数是否正确
若是res返回的是非false,而是一堆ldap数据,那么就说明ldap链接是正常的,那么下面开始使用ldap3联合django作认证吧mysql
若是上述步骤没有错误的话,那么请走下面这一步git
>>> user="cn=oliaojiaf,ou=People,dc=xxx,dc=com," # 错误写法 >>> user='cn=oliaojiaf,dc=xxx,dc=com,ou=People,' # 错误写法 >>> user='cn=oliaojiaf,ou=People,dc=xxx,dc=com' #正确的写法 >>> c = ldap3.Connection(ldap3.Server("ldap://192.168.2.1:389",get_info=ldap3.NONE,allowed_referral_hosts=[("*", True)],),user=user,password="ljf,xxx",auto_bind=ldap3.AUTO_BIND_NO_TLS,raise_exceptions=True,) >>> c.open() >>> c.bind() True
若是上面也是没有问题的话,那么就能够配置django+ldap认证了github
我们使用django-python3-ldap,因此按照安装配置启动三步走的方法来。
1.安装django-python3-ldap模块sql
pip install django-python3-ldap
2.配置
django-python3-ldap 模块 配置方法能够看下官网,官网django
AUTHENTICATION_BACKENDS = ( "django_python3_ldap.auth.LDAPBackend", #配置为先使用LDAP认证,如经过认证则再也不使用后面的认证方式 #'django.contrib.auth.backends.ModelBackend', ) LDAP_AUTH_URL = 'ldap://192.168.2.1:389' LDAP_AUTH_USE_TLS = False LDAP_AUTH_SEARCH_BASE = 'ou=People,dc=xxx,dc=com' LDAP_AUTH_OBJECT_CLASS = "inetOrgPerson" LDAP_AUTH_USER_FIELDS = { "username": "sn", "last_name": "sn", "first_name": "sn", "email": "mail" } LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data" LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations" LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters" LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_openldap"
3.修改django_python3_ldap.ldap的代码。
这一步我本身反复测试,发现这个包发给ldap-server的数据格式不对,致使ldap-server返回的就是invalidCredentials,因此咱们须要修改它的代码,使其符合ldap-server要求的数据格式,这个怎么修改就看本身的需求了,没有标准答案。
修改的代码相对路径是 安装django_python3_ldap的lib路径/django_python3_ldap/ldap
,例如个人是在 /usr/local/python356/lib/python3.5/site-packages/django_python3_ldap/ldap.py
,在 connection 的方法里面 ,在148行开始运维
username = username.replace("sn","cn") # 本身添加的代码
而后在183行注释掉源代码,添加本身的代码测试
try: # c.rebind( # user=format_username({User.USERNAME_FIELD: settings.LDAP_AUTH_CONNECTION_USERNAME}), # password=settings.LDAP_AUTH_CONNECTION_PASSWORD, # ) c.open() # 本身添加的代码 if not c.bind(): #bind 为false,说明认证失败,须要抛出异常 raise LDAPException # 本身添加的代码,bind返回true的话,说明用户信息认证成功 except LDAPException as ex:
之因此须要注释掉上面的代码,是由于rebind是借助已有的链接再次认证下,此次认证的是咱们在settings配置的用户名密码,因为我司运维同时给的ldap链接信息里没有CONNECTION_USERNAME,因此老是认证经过不了,因此我这里就须要注释了。code
而后重启djanog,发现就能认证成功了。orm