Python使用LDAP做用戶認(rèn)證的方法
LDAP(Light Directory Access Portocol)是輕量目錄訪問(wèn)協(xié)議,基于X.500標(biāo)準(zhǔn),支持TCP/IP。
LDAP目錄以樹(shù)狀的層次結(jié)構(gòu)來(lái)存儲(chǔ)數(shù)據(jù)。每個(gè)目錄記錄都有標(biāo)識(shí)名(Distinguished Name,簡(jiǎn)稱DN),用來(lái)讀取單個(gè)記錄,
一般是這樣的:
cn=username,ou=people,dc=test,dc=com
幾個(gè)關(guān)鍵字的含義如下:
- base dn:LDAP目錄樹(shù)的最頂部,也就是樹(shù)的根,是上面的dc=test,dc=com部分,一般使用公司的域名,也可以寫(xiě)做o=test.com,前者更靈活一些。
- dc::Domain Component,域名部分。
- ou:Organization Unit,組織單位,用于將數(shù)據(jù)區(qū)分開(kāi)。
- cn:Common Name,一般使用用戶名。
- uid:用戶id,與cn的作用類(lèi)似。
- sn:Surname, 姓。
- rdn:Relative dn,dn中與目錄樹(shù)的結(jié)構(gòu)無(wú)關(guān)的部分,通常存在cn或者uid這個(gè)屬性里。
所以上面的dn代表一條記錄,代表一位在test.com公司people部門(mén)的用戶username。
python-ldap
python一般使用python-ldap庫(kù)操作ldap,文檔:https://www.python-ldap.org/en/latest/index.html。
下載:
pip install python-ldap
還要安裝一些環(huán)境,ubuntu:
apt-get install build-essential python3-dev python2.7-dev \ libldap2-dev libsasl2-dev slapd ldap-utils python-tox \ lcov valgrind
CentOS:
yum groupinstall "Development tools" yum install openldap-devel python-devel
獲取LDAP地址后即可與LDAP建立連接:
import ldap
ldapconn = ldap.initialize('ldap://192.168.1.111:389')
綁定用戶,可用于用戶驗(yàn)證,用戶名必須是dn:
ldapconn.simple_bind_s('cn=username,ou=people,dc=test,dc=com', pwd)
成功認(rèn)證時(shí)會(huì)返回一個(gè)tuple:
(97, [], 1, [])
驗(yàn)證失敗會(huì)報(bào)異常ldap.INVALID_CREDENTIALS:
{'desc': u'Invalid credentials'}
注意驗(yàn)證時(shí)傳空值驗(yàn)證也是可以通過(guò)的,注意要對(duì)dn和pwd進(jìn)行檢查。
查詢LDAP用戶信息時(shí),需要登錄管理員RootDN帳號(hào):
ldapconn.simple_bind_s('cn=admin,dc=test,dc=com', 'adminpwd')
searchScope = ldap.SCOPE_SUBTREE
searchFilter = 'cn=username'
base_dn = 'ou=people,dc=test,dc=com'
print ldapconn.search_s(base_dn, searchScope, searchFilter, None)
添加用戶add_s(dn, modlist),dn為要添加的條目dn,modlist為存儲(chǔ)信息:
dn = 'cn=test,ou=people,dc=test,dc=com'
modlist = [
('objectclass', ['person', 'organizationalperson'],
('cn', ['test']),
('uid', [''testuid]),
('userpassword', ['pwd']),
]
result = ldapconn.add_s(dn, modlist)
添加成功會(huì)返回元組:
(105, [], 2, [])
失敗會(huì)報(bào)ldap.LDAPError異常
Django使用LDAP驗(yàn)證
一個(gè)很簡(jiǎn)單的LDAP驗(yàn)證Backend:
import ldap
class LDAPBackend(object):
"""
Authenticates with ldap.
"""
_connection = None
_connection_bound = False
def authenticate(self, username=None, passwd=None, **kwargs):
if not username or not passwd:
return None
if self._authenticate_user_dn(username, passwd):
user = self._get_or_create_user(username, passwd)
return user
else:
return None
@property
def connection(self):
if not self._connection_bound:
self._bind()
return self._get_connection()
def _bind(self):
self._bind_as(
LDAP_CONFIG['USERNAME'], LDAP_CONFIG['PASSWORD'], True
)
def _bind_as(self, bind_dn, bind_password, sticky=False):
self._get_connection().simple_bind_s(
bind_dn, bind_password
)
self._connection_bound = sticky
def _get_connection(self):
if not self._connection:
self._connection = ldap.initialize(LDAP_CONFIG['HOST'])
return self._connection
def _authenticate_user_dn(self, username, passwd):
bind_dn = 'cn=%s,%s' % (username, LDAP_CONFIG['BASE_DN'])
try:
self._bind_as(bind_dn, passwd, False)
return True
except ldap.INVALID_CREDENTIALS:
return False
def _get_or_create_user(self, username, passwd):
# 獲取或者新建User
return user
不想自己寫(xiě)的話,django與flask都有現(xiàn)成的庫(kù):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)身份證實(shí)名認(rèn)證的方法實(shí)例
- 微信小程序python用戶認(rèn)證的實(shí)現(xiàn)
- python連接mongodb密碼認(rèn)證實(shí)例
- python pycurl驗(yàn)證basic和digest認(rèn)證的方法
- python2.7+selenium2實(shí)現(xiàn)淘寶滑塊自動(dòng)認(rèn)證功能
- Python3中使用urllib的方法詳解(header,代理,超時(shí),認(rèn)證,異常處理)
- 將Python的Django框架與認(rèn)證系統(tǒng)整合的方法
- Python使用htpasswd實(shí)現(xiàn)基本認(rèn)證授權(quán)的例子
- Python如何實(shí)現(xiàn)后端自定義認(rèn)證并實(shí)現(xiàn)多條件登陸
相關(guān)文章
python3 tkinter實(shí)現(xiàn)添加圖片和文本
這篇文章主要為大家詳細(xì)介紹了python3 tkinter實(shí)現(xiàn)添加圖片和文本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢實(shí)例
這篇文章主要介紹了Python ORM框架SQLAlchemy學(xué)習(xí)筆記之安裝和簡(jiǎn)單查詢實(shí)例,簡(jiǎn)明入門(mén)教程,需要的朋友可以參考下2014-06-06
關(guān)于matplotlib-legend 位置屬性 loc 使用說(shuō)明
這篇文章主要介紹了關(guān)于matplotlib-legend 位置屬性 loc 使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python+Tinify實(shí)現(xiàn)高效批量壓縮圖片
這篇文章主要為大家詳細(xì)介紹了Python如何利用Tinify實(shí)現(xiàn)高效批量壓縮圖片功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-02-02
使用python查找windows系統(tǒng)中所有程序的安裝信息
這篇文章主要為大家介紹了使用python查找windows系統(tǒng)中所有程序的安裝信息示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
使用python將大量數(shù)據(jù)導(dǎo)出到Excel中的小技巧分享
今天小編就為大家分享一篇使用python將大量數(shù)據(jù)導(dǎo)出到Excel中的小技巧心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Python urllib.request對(duì)象案例解析
這篇文章主要介紹了Python urllib.request對(duì)象案例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
對(duì)python多線程中Lock()與RLock()鎖詳解
今天小編就為大家分享一篇對(duì)python多線程中Lock()與RLock()鎖詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

