python3 使用ldap3来作为django认证后台

首先先使用ldap3测试ldap服务是否正常

我们先要拿到dc的数据,以及连接ldap的密码,还有搜索的字段(search_filter), 一般来说search_filter 这个是从负责ldap运维的同事获取的。attributes 这个是获取哪些字段的数据,犹如mysql 语句的select xx,xxx , 如果吧attributes设置为ALL_ATTRIBUTES,那么就是获取所有字段数据。

search_base='ou=xx,dc=xxx,dc=com'  # ldap运维同事提供
ldaphost = ("ldap://192.168.2.1:389")
password="11111"    # 这个是管理员的密码
search_filter="(objectclass=*)"  

from ldap3 import Server, Connection, ALL, SUBTREE, ServerPool,ALL_ATTRIBUTES
s = Server(ldaphost)
conn = Connection(s,password=password)  
conn.open()
conn.bind()
True  # 执行完bind方法后,返回true说明正确,如果是false的话,需要检查你Connection参数是否正确
res = conn.search(search_base=search_base,search_filter=search_filter,search_scope=SUBTREE,attributes = ALL_ATTRIBUTES)    # 如果res不是false说明search方法执行成功啦。否则检查传入的参数是否正确

如果res返回的是非false,而是一堆ldap数据,那么就说明ldap连接是正常的,那么下面开始使用ldap3联合django做认证吧

如果上述步骤没有错误的话,那么请走下面这一步

>>> user="cn=oliaojiaf,ou=People,dc=xxx,dc=com,"  # 错误写法
>>> user='cn=oliaojiaf,dc=xxx,dc=com,ou=People,'  # 错误写法
>>> user='cn=oliaojiaf,ou=People,dc=xxx,dc=com' #正确的写法
>>> c = ldap3.Connection(ldap3.Server("ldap://192.168.2.1:389",get_info=ldap3.NONE,allowed_referral_hosts=[("*", True)],),user=user,password="ljf,xxx",auto_bind=ldap3.AUTO_BIND_NO_TLS,raise_exceptions=True,)
>>> c.open()
>>> c.bind()
True

如果上面也是没有问题的话,那么就可以配置django+ldap认证了

python3 django ldap认证

咱们使用django-python3-ldap,所以按照安装配置启动三步走的方法来。
1.安装django-python3-ldap模块

pip install django-python3-ldap

2.配置
django-python3-ldap 模块 配置方法可以看下官网,官网

AUTHENTICATION_BACKENDS = (
    "django_python3_ldap.auth.LDAPBackend",  #配置为先使用LDAP认证,如通过认证则不再使用后面的认证方式
    #'django.contrib.auth.backends.ModelBackend',
)
LDAP_AUTH_URL = 'ldap://192.168.2.1:389'
LDAP_AUTH_USE_TLS = False
LDAP_AUTH_SEARCH_BASE = 'ou=People,dc=xxx,dc=com'
LDAP_AUTH_OBJECT_CLASS = "inetOrgPerson"
LDAP_AUTH_USER_FIELDS  = {
    "username": "sn",
    "last_name": "sn",
    "first_name": "sn",
    "email": "mail"
}
LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"
LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"
LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters"
LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_openldap"

3.修改django_python3_ldap.ldap的代码。
这一步我自己反复测试,发现这个包发给ldap-server的数据格式不对,导致ldap-server返回的就是invalidCredentials,所以我们需要修改它的代码,使其符合ldap-server要求的数据格式,这个怎么修改就看自己的需求了,没有标准答案。
修改的代码相对路径是 安装django_python3_ldap的lib路径/django_python3_ldap/ldap ,例如我的是在 /usr/local/python356/lib/python3.5/site-packages/django_python3_ldap/ldap.py,在 connection 的方法里面 ,在148行开始

username = username.replace("sn","cn")  # 自己添加的代码

然后在183行注释掉源代码,添加自己的代码

try:
    # c.rebind(
    #     user=format_username({User.USERNAME_FIELD: settings.LDAP_AUTH_CONNECTION_USERNAME}),
    #     password=settings.LDAP_AUTH_CONNECTION_PASSWORD,
    # )
    c.open()  # 自己添加的代码
    if not c.bind():  #bind 为false,说明认证失败,需要抛出异常
        raise LDAPException # 自己添加的代码,bind返回true的话,说明用户信息认证成功
except LDAPException as ex:

之所以需要注释掉上面的代码,是因为rebind是借助已有的连接再次认证下,这次认证的是我们在settings配置的用户名密码,由于我司运维同时给的ldap连接信息里没有CONNECTION_USERNAME,所以总是认证通过不了,所以我这里就需要注释了。

然后重启djanog,发现就能认证成功了。

转载于:https://www.cnblogs.com/liaojiafa/p/9995335.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值