python3 使用ldap3來做爲django認證後臺

首先先使用ldap3測試ldap服務是否正常

咱們先要拿到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

python3 django ldap認證

我們使用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

相關文章
相關標籤/搜索