python登陆页面后ldap3跳转_如何使用Python连接ldap

本文介绍如何使用Python的ldap3库连接LDAP服务器进行用户登录验证,并通过源码分析理解其过程。首先,展示如何建立连接并测试认证,接着详细解释搜索用户、验证密码的步骤。最后,探讨获取用户所属组的方法,以便于权限管理。
摘要由CSDN通过智能技术生成

如何使用Python连接ldap

好多使用ldap认证的软件都是Python的,比如superset和airflow, 好吧,他们都是airbnb家的。在配置ldap的时候可能会出现认证失败,你不知道是因为什么导致配置失败的。所以,就要

跟踪源码,看看内部怎么认证实现的。

具体来实现如下:

启动

docker run -it ryan/python:3 /bin/bash

下载ldap3

pip install ldap3

测试连接

root@5edee218d962:/# python

Python 3.7.4 (default, Jul 13 2019, 14:20:24)

[GCC 6.3.0 20170516] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from ldap3 import Server, Connection, ALL, NTLM, ALL_ATTRIBUTES, LEVEL, SUBTREE

>>> server = Server('172.17.0.2', get_info=ALL)

>>> conn = Connection(server, 'cn=admin,dc=demo,dc=com', 'admin', auto_bind=True)

>>> conn.extend.standard.who_am_i()

'dn:cn=admin,dc=demo,dc=com'

测试登录部分

登录源码如下:

@staticmethod

def try_login(username, password):

conn = get_ldap_connection(configuration.conf.get("ldap", "bind_user"),

configuration.conf.get("ldap", "bind_password"))

search_filter = "(&({0})({1}={2}))".format(

configuration.conf.get("ldap", "user_filter"),

configuration.conf.get("ldap", "user_name_attr"),

username

)

search_scope = LEVEL

if configuration.conf.has_option("ldap", "search

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值