本文詳細介紹了django-auth-ldap的使用方法,參數含義,并提供了示例代碼
版本說明
- django==2.2
- django-auth-ldap==1.7.0
集成過程
django集成ldap認證有現成的django-auth-ldap模塊可以使用,本文也主要以這個模塊的使用為主,先安裝模塊
1
|
pip install django - auth - ldap |
然后在setting.py全局配置文件中添加如下內容就可以正常使用了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import ldap from django_auth_ldap.config import ldapsearch, groupofnamestype # baseline configuration. auth_ldap_server_uri = 'ldap://ldap.ops-coffee.cn' auth_ldap_bind_dn = 'uid=authz,ou=public,dc=ops-coffee,dc=cn' auth_ldap_bind_password = 'czfdx629k7' auth_ldap_user_search = ldapsearch( 'ou=people,dc=ops-coffee,dc=cn' , ldap.scope_subtree, '(uid=%(user)s)' , ) # or: # auth_ldap_user_dn_template = 'uid=%(user)s,ou=people,dc=ops-coffee,dc=cn' auth_ldap_user_attr_map = { 'first_name' : 'cn' , 'last_name' : 'sn' , 'email' : 'mail' , } authentication_backends = ( 'django_auth_ldap.backend.ldapbackend' , 'django.contrib.auth.backends.modelbackend' , ) |
這里詳細解釋下上邊配置的含義:
auth_ldap_server_uri: ldap服務器的地址
auth_ldap_bind_dn: 一個完整的用戶dn,用來登錄ldap服務器驗證用戶輸入的賬號密碼信息是否正確
auth_ldap_bind_password: bind_dn用戶的密碼,這里我們簡單說明下ldap的認證邏輯以便更好的理解為啥需要這兩個配置
django使用auth_ldap_bind_dn和auth_ldap_bind_password作為用戶名和密碼登陸ldap服務器,根據auth_ldap_user_search指定的查詢規則來查找用戶輸入的屬性(即username)的值有沒有,如果查找的條數為0或者大于1,則返回錯誤,如果查找的條數等于1,則使用查找到的這個條目的dn和用戶輸入的密碼進行匹配驗證,成功則返回成功允許登錄,失敗則不允許登錄
auth_ldap_user_search: 可通過ldap登錄的用戶的范圍,如上配置會去ou=people,dc=ops-coffee,dc=cn下搜索用戶是否存在
其中(uid=%(user)s)'指明了作為django的username所對應的ldap的屬性,這里為ldap用戶的uid屬性作為django的username
以上配置是在一個ou下查找用戶,當需要在多個ou下搜索用戶時用如下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from django_auth_ldap.config import ldapsearch, ldapsearchunion auth_ldap_user_search = ldapsearchunion( ldapsearch( 'ou=public,dc=ops-coffee,dc=cn' , ldap.scope_subtree, '(uid=%(user)s)' ), ldapsearch( 'ou=people,dc=ops-coffee,dc=cn' , ldap.scope_subtree, '(uid=%(user)s)' ), ) |
auth_ldap_user_attr_map: ldap中的用戶屬性跟django后臺用戶屬性的對應關系,當用戶第一次登錄且驗證成功后會將ldap中對應的用戶屬性寫入到django的user表中
authentication_backends: 配置django的后端認證列表
當django調用auth.authenticate方法進行驗證時,django將嘗試authentication_backends元組中指定的所有認證后端。如果第一個認證方法失敗了,django將會繼續嘗試下一個,直到所有認證方式都嘗試完成
django默認的認證后端是django.contrib.auth.backends.modelbackend,如上配置我們添加了ldap的認證到authentication_backends中,那么django在登錄的時候就會先去ldap服務器驗證用戶,驗證失敗后再去查詢本地數據庫的user表進行驗證,如果只希望django驗證ldap不驗證本地數據庫的話去掉authentication_backends中的modelbackend配置即可
其他幾個django-auth-ldap的全局配置參數解釋如下:
auth_ldap_always_update_user: 是否同步ldap的修改,默認為true,即當ldap中用戶的屬性修改后用戶通過ldap系統認證時自動同步更新到django的user表中,如果設置為false則不自動更新
auth_ldap_cache_timeout: 設置ldap認證緩存的時間
登錄驗證
上邊的配置沒有問題后就可以通過ldap系統賬號進行登錄操作了,默認登陸邏輯及前端登錄代碼均無需修改,可以參考github的相關代碼,地址:
高級配置
所謂高級配置這里主要是說明下django-auth-ldap
中組相關的配置,這需要對ldap的組有一定的概念,為了方便理解,接下來我們以實際的例子來說明
假如我們有三個組overmind、kerrigan、admin,配置如下:
1
2
3
4
5
6
|
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=overmind,ou=group,dc=ops-coffee,dc=cn dn: cn = overmind,ou = group,dc = ops - coffee,dc = cn cn: overmind member: uid = sre,ou = people,dc = ops - coffee,dc = cn objectclass: groupofnames objectclass: top |
1
2
3
4
5
6
7
|
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=kerrigan,ou=group,dc=ops-coffee,dc=cn dn: cn = kerrigan,ou = group,dc = ops - coffee,dc = cn cn: kerrigan objectclass: groupofnames objectclass: top member: uid = u1,ou = public,dc = ops - coffee,dc = cn member: uid = u2,ou = people,dc = ops - coffee,dc = cn |
1
2
3
4
5
6
|
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=admin,ou=group,dc=ops-coffee,dc=cn dn: cn = admin,ou = group,dc = ops - coffee,dc = cn cn: admin member: uid = u3,ou = admin,dc = ops - coffee,dc = cn objectclass: groupofnames objectclass: top |
我們需要實現django集成ldap認證,且不允許隸屬于kerrigan分組的用戶登錄系統,如果用戶隸屬于admin分組,則需要在登錄django時給設置為管理員,接下來的配置將會解釋如何實現該需求
django-auth-ldap中與group有關的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
auth_ldap_group_search = ldapsearch( 'ou=group,dc=ops-coffee,dc=cn' , ldap.scope_subtree, '(objectclass=groupofnames)' , ) auth_ldap_group_type = groupofnamestype(name_attr = 'cn' ) # simple group restrictions # auth_ldap_require_group = 'cn=overmind,ou=group,dc=ops-coffee,dc=cn' auth_ldap_deny_group = 'cn=kerrigan,ou=group,dc=ops-coffee,dc=cn' auth_ldap_user_flags_by_group = { 'is_superuser' : 'cn=admin,ou=group,dc=ops-coffee,dc=cn' , } |
以上配置的詳細解釋如下:
auth_ldap_group_search: 搜索某個ou下的信息,與auth_ldap_user_search參數類似,這里的ou一般指group,例如ou=group,dc=ops-coffee,dc=cn的組目錄
auth_ldap_group_type: 返回的組的類型,組dn的第一個屬性值,例如組dncn=overmind,ou=group,dc=ops-coffee,dc=cn,那么這里為cn
auth_ldap_require_group: 設置允許哪些組成員登錄,如果我們只允許overmind組的成員可以登錄系統的話這里可以設置
1
|
auth_ldap_require_group = 'cn=overmind,ou=group,dc=ops-coffee,dc=cn' |
auth_ldap_deny_group: 設置拒絕哪些組成員登錄,如果我們不允許kerrigan組的成員可以登錄系統的話這里可以設置
1
|
auth_ldap_deny_group = 'cn=kerrigan,ou=group,dc=ops-coffee,dc=cn' |
當我們同時設置了用戶既屬于overmind組又屬于kerrigan組,也就是這個用戶即設置了允許登錄,又設置了拒絕登錄,那么以拒絕登錄為準,用戶無法登錄
auth_ldap_user_flags_by_group: 根據ldap的group設置django用戶的額外屬性,例如我們想要設置ldap中
admin組具有django中超級管理員的權限,除了在django中手動設置外,還可以直接在setting中配置
1
2
3
4
|
auth_ldap_user_flags_by_group auth_ldap_user_flags_by_group = { 'is_superuser' : 'cn=admin,ou=group,dc=ops-coffee,dc=cn' , } |
當admin組用戶登錄的時候就會自動給用戶的is_superuser屬性設置為true
至此我們對django-auth-ldap有了一個全面的了解,在實際項目集成中可以做到游刃有余,如有問題可以參考我github的代碼
踩坑記錄
windowns 10下安裝python-ldap即django-auth-ldap報錯:
c:\users\ops-coffee\appdata\local\temp\pip-install-sec1o036\python-ldap\modules\constants.h(7): fatal error c1083: cannot open include file: 'lber.h': no such file or directory
error: command 'c:\\program files (x86)\\microsoft visual studio 14.0\\vc\\bin\\x86_amd64\\cl.exe' failed with exit status 2
這個報錯需要手動安裝下whl文件,具體方法為:
先在這個網站下載對應版本的python-ldap的whl文件
然后使用pip命令安裝whl,注意文件路徑要正確
1
2
3
4
5
6
|
d:\demo\openldap>python - m pip install python_ldap - 3.2 . 0 - cp36 - cp36m - win_amd64.whl processing d:\demo\openldap\python_ldap - 3.2 . 0 - cp36 - cp36m - win_amd64.whl requirement already satisfied: pyasn1> = 0.3 . 7 in c:\python36\lib\site - packages ( from python - ldap = = 3.2 . 0 ) ( 0.4 . 2 ) requirement already satisfied: pyasn1 - modules> = 0.1 . 5 in c:\python36\lib\site - packages ( from python - ldap = = 3.2 . 0 ) ( 0.2 . 4 ) installing collected packages: python - ldap successfully installed python - ldap - 3.2 . 0 |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/db62586b438f