1.安裝python-ldap(python_ldap-2.4.25-cp27-none-win_amd64.whl)pip install python_ldap-2.4.25-cp27-none-win_amd64.whl
2.安裝django-auth-ldap(django-auth-ldap-1.2.8.tar.gz)(下載:https://pypi.python.org/pypi/django-auth-ldap),windows下也可以使用 python setup.py install
安裝成功后運行命令,運行成功表示安裝成功
1
|
from django_auth_ldap.config import ldapsearch, ldapsearchunion, groupofnamestype |
3.配置settings.py,增加如下:
參考:https://pypi.python.org/pypi/django-auth-ldap/1.2.1說明文檔
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
28
29
30
31
32
33
34
|
# -*- coding: utf-8 -*- import ldap from django_auth_ldap.config import ldapsearch #導入ldap model authentication_backends = ( 'django_auth_ldap.backend.ldapbackend' , #配置為先使用ldap認證,如通過認證則不再使用后面的認證方式 'django.contrib.auth.backends.modelbackend' , ) auth_ldap_server_uri = 'ldap://192.168.200.20:389' auth_ldap_bind_dn = 'cn=test01,ou=serveradmin,dc=uu,dc=yyy,dc=com' auth_ldap_bind_password = '123456' ou = unicode ( 'ou=中文名,dc=uu,dc=yyy,dc=com' , 'utf8' ) #限制哪個ou中的用戶可以進行ad認證。如果ou中包含有中文字符,則需要這樣寫,否則會出現ascii無法識別的報錯(unicodedecodeerror: 'ascii' codec can't decode byte 0xc3 in position) # ou0 = 'ou=serveradmin,dc=uxin,dc=youxinpai,dc=com' # ou = unicode('ou=優,dc=uxin,dc=youxinpai,dc=com', 'utf8') # ou1 = u'ou=優,dc=uxin,dc=youxinpai,dc=com' # ou2 = u'ou=\u4f18,dc=uxin,dc=youxinpai,dc=com' # ou == ou1 == ou2 #返回true #檢索單個ou auth_ldap_user_search = ldapsearch(ou, ldap.scope_subtree, "(&(objectclass=person)(samaccountname=%(user)s))" ) # 檢索多個ou: # auth_ldap_user_search = ldapsearchunion( # ldapsearch("ou=user,ou=ou1,ou=ou,dc=cn,dc=com",ldap.scope_subtree, "(&(objectclass=user)(samaccountname=%(user)s))"), # ldapsearch("ou=user,ou=ou2,ou=ou,dc=cn,dc=com",ldap.scope_subtree, "(&(objectclass=user)(samaccountname=%(user)s))"), # ) #將賬號的姓、名、郵件地址保存到django的auth_user表中,在admin后臺可以看到 auth_ldap_user_attr_map = { "first_name" : "givenname" , "last_name" : "sn" , "email" : "mail" } |
同步用戶組信息:
當用戶登錄后,如果用戶屬于某個組,則會將該組同步到auth_group表中,之后在admin后臺可以對該組進行權限設置,之后同屬于該組的用戶在登錄后則具有相應的權限。
當一個用戶不再屬于某個組,該組也不會被自動刪掉,在admin后臺手工刪掉即可。
1
2
3
4
5
6
7
8
9
10
|
from django_auth_ldap.config import ldapsearch, ldapsearchunion, groupofnamestype auth_ldap_group_type = groupofnamestype(name_attr = "cn" ) #返回的組的類型,并用來判斷用戶與組的從屬關系 oug = unicode ( 'ou=安全組,dc=uu,dc=yyy,dc=com' , 'utf8' ) auth_ldap_group_search = ldapsearch(oug,ldap.scope_subtree, "(objectclass=group)" ) #搜索某個ou下組信息 auth_ldap_mirror_groups = true #導入用戶的組信息,在用戶登錄的時候把用戶的域組關系同步過來。每次用戶登錄時,都會把用戶的組關系刪除,重新從ldap中進行同步(解決辦法參考后面) auth_ldap_always_update_user = true #是否同步ldap修改 |
4.編輯views.py,當用戶通過認證后,還可以使用django自帶的用戶認證、權限設置模塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from django.contrib.auth import authenticate,login as auth_login,logout as auth_logout from django.contrib.auth.models import user @csrf_exempt def loginauth(request): user_loggedin = 'guest' errors_list = [] if request.method = = 'post' : print 'pp: ' ,request.post.get( 'name' ),request.post.get( 'password' ) name = request.post.get( 'name' ) password = request.post.get( 'password' ) user = authenticate(username = name, password = password) print 'authuser' ,user if user is not none: auth_login(request,user) uu = request.user u = user.objects.get(username = uu) return httpresponseredirect( "../check_dict" ) context = { 'errors_list' :errors_list, 'user_loggedin' :user_loggedin} return render(request, 'aptest/loginauth.html' ,context) |
auth_user表結構:
admin后臺顯示:
解決中文亂碼問題(有問題可以試下):
在安裝django-auth-ldap-1.2.8.tar之前,先在里面的.py中加上'# -*- coding: utf-8 -*-'
修改c:\python27\lib\site-packages\django-1.8.4-py2.7.egg\django\conf\global_settings.py和修改settings.py,如下:
1
2
|
time_zone = 'asia/shanghai' language_code = 'zh-hans' |
======================== ========================
ldap用戶驗證基本原理
每個用戶在ldap系統中有一個唯一的dn值,例如配置文件中默認的admin用戶在ldap中的dn值是uid=admin,ou=system,dc=eoncloud,dc=com, 其中eoncloud.com是域名,system是組名,admin是用戶名,有些ldap用cn而不是uid來生成dn,在這種系統中admin的dn看起來像這樣cn=admin,ou=system,dc=eoncloud,dc=com,無論是uid還是cn或是別的前綴,django-ldap-auth都是用dn來驗證用戶和獲取用戶信息的.
假設用戶輸入的帳號及密碼是: test, password.
django-auth-ldap有2個方式來獲取用戶的dn
- 使用auth_ldap_user_dn_template提供的模板生成dn.如uid=%(user)s,ou=users,dc=eoncloud,dc=com, 其中%(user)s會被替換成用戶名,這樣最終的dn就是uid=test,ou=users,dc=eonclooud,dc=com.
- 使用auth_ldap_group_search.如果沒有配置auth_ldap_user_dn_template,那么django-auth-ldap會使用auth_ldap_bind_dn和auth_ldap_bind_password提供的dn與密碼根據auth_ldap_group_search提供的查詢條件去查找test用戶,如果查不到,驗證失敗,如果查到用戶,就使用返回的數據生成test的dn.
- 利用第2步生成dn值與密碼嘗試訪問ldap系統,如果訪問成功,則驗證共過,否則驗證失敗.
基本配置
- auth_ldap_server_uri. ldap系統的地址及端口號
- auth_ldap_bind_dn, auth_ldap_bind_password. 查找用戶及相關信息的默認用戶信息
- auth_ldap_user_search. 第一個參數指指定詢目錄,第三個參數是過濾條件,過濾條件可以很復雜,有需要請查看相關文檔.
- auth_ldap_user_dn_template. 用戶dn模板,配置該參數后django-auth-ldap會用生成的dn配合密碼驗證該用戶.
- auth_ldap_user_attr_map. ldap與user model映射.
- auth_ldap_always_update_user. 是否同步ldap修改.
用戶組配置
如果需要,django-auth-ldap可以從ldap系統獲取用戶的組信息,也可以限定某個組里的用戶訪問,或者阻止某個組里的用戶訪問,無論是使用哪個功能都需要先配置組類型auth_ldap_group_type及auth_ldap_group_search, 因為ldap里組的種類非常多,具體信息請查詢相關資料.
auth_ldap_group_type
- 值類型: ldapgrouptype的子類實例.ldapgrouptype有2個初始化參數:member_attr, name_attr.member_attr是組成員的屬性名, name_attr是組名稱的屬性名.
- 作用: auth_ldap_group_search返回的組的類型,并用來判斷用戶與組的從屬關系
auth_ldap_group_search
- 值類型: ldapsearch實例.
- 作用: 用戶組的查詢條件
auth_ldap_require_group
- 值類型: 組的dn
- 作用: 只有指定組的用戶可以訪問
auth_ldap_deny_group指定的
- 值類型: 組的dn
- 作用: 禁止指定組的用戶訪問
auth_ldap_mirror_groups
- 值類型: bool值
- 作用: 導入用戶的組信息
auth_ldap_mirror_groups=true 這個參數是為了在用戶登錄的時候把用戶的域組關系也獲取并記錄下來。不過開啟這個參數會帶來另外一個問題:每次用戶登錄時,都會把用戶的組關系刪除,重新從ldap中進行同步。由于我們的系統中除了域組還有些自定義的組關系,這樣一來自定義組的用戶關系就不能持久保留了。按照我們的需求,其實只希望在用戶第一次登錄的時候同步組關系,以后的登錄并不需要。這個需求可以通過對django-auth-ldap的源碼(backend.py)進行微調來實現。
backend.py源碼:
1
2
3
4
5
6
7
8
9
|
def _get_or_create_user( self , force_populate = false): ... ... if self .settings.mirror_groups: self ._mirror_groups() #修改為如下,然后重新安裝django-auth-ldap-1.2.8.tar,重啟web重新驗證即可。 if self .settings.mirror_groups and created: self ._mirror_groups() |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/dreamer-fish/p/5474289.html