使用ldap对接linux
时间: 2023-12-07 13:27:50 浏览: 105
使用LDAP对接Linux是一种常见的用户认证方式。可以参考中介绍的配置Linux使用LDAP用户认证的方法进行操作。下面提供一个简单的示例代码供参考:
```python
import ssl
from ldap3 import Server, Connection
SERVER = "ldap.example.com"
USER = "cn=admin,dc=example,dc=com"
PASSWORD = "password"
BASE_DN = "dc=example,dc=com"
def ldap_connect():
server = Server(SERVER, port=636, use_ssl=True, get_info="ALL",
tls=ssl.TLS(validate=ssl.CERT_REQUIRED, version=ssl.PROTOCOL_TLSv1, ciphers="ALL"))
conn = Connection(server, user=USER, password=PASSWORD)
conn.bind()
return conn
def ldap_search(username):
conn = ldap_connect()
conn.search(BASE_DN, f"(uid={username})")
return conn.entries
def ldap_authenticate(username, password):
conn = ldap_connect()
conn.search(BASE_DN, f"(uid={username})")
if len(conn.entries) == 1:
user_dn = conn.entries
阅读全文