脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Django项目中添加ldap登陆认证功能的实现

Django项目中添加ldap登陆认证功能的实现

2021-06-12 00:38brownchen Python

这篇文章主要介绍了Django项目中添加ldap登陆认证功能的实现,详细介绍了django-auth-ldap的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文详细介绍了django-auth-ldap的使用方法,参数含义,并提供了示例代码

版本说明

  • django==2.2
  • django-auth-ldap==1.7.0

集成过程

django集成ldap认证有现成的django-auth-ldap模块可以使用,本文也主要以这个模块的使用为主,先安装模块

?
1
pip install django-auth-ldap

然后在setting.py全局配置文件中添加如下内容就可以正常使用了:

?
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
import ldap
from django_auth_ldap.config import ldapsearch, groupofnamestype
 
# baseline configuration.
auth_ldap_server_uri = 'ldap://ldap.ops-coffee.cn'
 
auth_ldap_bind_dn = 'uid=authz,ou=public,dc=ops-coffee,dc=cn'
auth_ldap_bind_password = 'czfdx629k7'
 
auth_ldap_user_search = ldapsearch(
  'ou=people,dc=ops-coffee,dc=cn',
  ldap.scope_subtree,
  '(uid=%(user)s)',
)
# or:
# auth_ldap_user_dn_template = 'uid=%(user)s,ou=people,dc=ops-coffee,dc=cn'
 
auth_ldap_user_attr_map = {
  'first_name': 'cn',
  'last_name': 'sn',
  'email': 'mail',
}
 
authentication_backends = (
  'django_auth_ldap.backend.ldapbackend',
  'django.contrib.auth.backends.modelbackend',
)

这里详细解释下上边配置的含义:

auth_ldap_server_uri: ldap服务器的地址

auth_ldap_bind_dn: 一个完整的用户dn,用来登录ldap服务器验证用户输入的账号密码信息是否正确

auth_ldap_bind_password: bind_dn用户的密码,这里我们简单说明下ldap的认证逻辑以便更好的理解为啥需要这两个配置

django使用auth_ldap_bind_dn和auth_ldap_bind_password作为用户名和密码登陆ldap服务器,根据auth_ldap_user_search指定的查询规则来查找用户输入的属性(即username)的值有没有,如果查找的条数为0或者大于1,则返回错误,如果查找的条数等于1,则使用查找到的这个条目的dn和用户输入的密码进行匹配验证,成功则返回成功允许登录,失败则不允许登录

auth_ldap_user_search: 可通过ldap登录的用户的范围,如上配置会去ou=people,dc=ops-coffee,dc=cn下搜索用户是否存在

其中(uid=%(user)s)'指明了作为django的username所对应的ldap的属性,这里为ldap用户的uid属性作为django的username

以上配置是在一个ou下查找用户,当需要在多个ou下搜索用户时用如下配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from django_auth_ldap.config import ldapsearch, ldapsearchunion
 
auth_ldap_user_search = ldapsearchunion(
  ldapsearch(
    'ou=public,dc=ops-coffee,dc=cn',
    ldap.scope_subtree,
    '(uid=%(user)s)'
  ),
  ldapsearch(
    'ou=people,dc=ops-coffee,dc=cn',
    ldap.scope_subtree,
    '(uid=%(user)s)'
  ),
)

auth_ldap_user_attr_map: ldap中的用户属性跟django后台用户属性的对应关系,当用户第一次登录且验证成功后会将ldap中对应的用户属性写入到django的user表中

authentication_backends: 配置django的后端认证列表

当django调用auth.authenticate方法进行验证时,django将尝试authentication_backends元组中指定的所有认证后端。如果第一个认证方法失败了,django将会继续尝试下一个,直到所有认证方式都尝试完成

django默认的认证后端是django.contrib.auth.backends.modelbackend,如上配置我们添加了ldap的认证到authentication_backends中,那么django在登录的时候就会先去ldap服务器验证用户,验证失败后再去查询本地数据库的user表进行验证,如果只希望django验证ldap不验证本地数据库的话去掉authentication_backends中的modelbackend配置即可

其他几个django-auth-ldap的全局配置参数解释如下:

auth_ldap_always_update_user: 是否同步ldap的修改,默认为true,即当ldap中用户的属性修改后用户通过ldap系统认证时自动同步更新到django的user表中,如果设置为false则不自动更新

auth_ldap_cache_timeout: 设置ldap认证缓存的时间

登录验证

上边的配置没有问题后就可以通过ldap系统账号进行登录操作了,默认登陆逻辑及前端登录代码均无需修改,可以参考github的相关代码,地址:

高级配置

所谓高级配置这里主要是说明下django-auth-ldap中组相关的配置,这需要对ldap的组有一定的概念,为了方便理解,接下来我们以实际的例子来说明

假如我们有三个组overmind、kerrigan、admin,配置如下:

?
1
2
3
4
5
6
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=overmind,ou=group,dc=ops-coffee,dc=cn
dn: cn=overmind,ou=group,dc=ops-coffee,dc=cn
cn: overmind
member: uid=sre,ou=people,dc=ops-coffee,dc=cn
objectclass: groupofnames
objectclass: top
?
1
2
3
4
5
6
7
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=kerrigan,ou=group,dc=ops-coffee,dc=cn
dn: cn=kerrigan,ou=group,dc=ops-coffee,dc=cn
cn: kerrigan
objectclass: groupofnames
objectclass: top
member: uid=u1,ou=public,dc=ops-coffee,dc=cn
member: uid=u2,ou=people,dc=ops-coffee,dc=cn
?
1
2
3
4
5
6
# ldapsearch -lll -x -d "uid=authz,ou=public,dc=ops-coffee,dc=cn" -w "czfdx629k7" -b cn=admin,ou=group,dc=ops-coffee,dc=cn
dn: cn=admin,ou=group,dc=ops-coffee,dc=cn
cn: admin
member: uid=u3,ou=admin,dc=ops-coffee,dc=cn
objectclass: groupofnames
objectclass: top

我们需要实现django集成ldap认证,且不允许隶属于kerrigan分组的用户登录系统,如果用户隶属于admin分组,则需要在登录django时给设置为管理员,接下来的配置将会解释如何实现该需求

django-auth-ldap中与group有关的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
auth_ldap_group_search = ldapsearch(
  'ou=group,dc=ops-coffee,dc=cn',
  ldap.scope_subtree,
  '(objectclass=groupofnames)',
)
auth_ldap_group_type = groupofnamestype(name_attr='cn')
 
# simple group restrictions
# auth_ldap_require_group = 'cn=overmind,ou=group,dc=ops-coffee,dc=cn'
auth_ldap_deny_group = 'cn=kerrigan,ou=group,dc=ops-coffee,dc=cn'
 
auth_ldap_user_flags_by_group = {
  'is_superuser': 'cn=admin,ou=group,dc=ops-coffee,dc=cn',
}

以上配置的详细解释如下:

auth_ldap_group_search: 搜索某个ou下的信息,与auth_ldap_user_search参数类似,这里的ou一般指group,例如ou=group,dc=ops-coffee,dc=cn的组目录

auth_ldap_group_type: 返回的组的类型,组dn的第一个属性值,例如组dncn=overmind,ou=group,dc=ops-coffee,dc=cn,那么这里为cn

auth_ldap_require_group: 设置允许哪些组成员登录,如果我们只允许overmind组的成员可以登录系统的话这里可以设置

?
1
auth_ldap_require_group = 'cn=overmind,ou=group,dc=ops-coffee,dc=cn'

auth_ldap_deny_group: 设置拒绝哪些组成员登录,如果我们不允许kerrigan组的成员可以登录系统的话这里可以设置

?
1
auth_ldap_deny_group = 'cn=kerrigan,ou=group,dc=ops-coffee,dc=cn'

当我们同时设置了用户既属于overmind组又属于kerrigan组,也就是这个用户即设置了允许登录,又设置了拒绝登录,那么以拒绝登录为准,用户无法登录

auth_ldap_user_flags_by_group: 根据ldap的group设置django用户的额外属性,例如我们想要设置ldap中

admin组具有django中超级管理员的权限,除了在django中手动设置外,还可以直接在setting中配置

?
1
2
3
4
auth_ldap_user_flags_by_group
auth_ldap_user_flags_by_group = {
  'is_superuser': 'cn=admin,ou=group,dc=ops-coffee,dc=cn',
}

当admin组用户登录的时候就会自动给用户的is_superuser属性设置为true

至此我们对django-auth-ldap有了一个全面的了解,在实际项目集成中可以做到游刃有余,如有问题可以参考我github的代码

踩坑记录

windowns 10下安装python-ldap即django-auth-ldap报错:

c:\users\ops-coffee\appdata\local\temp\pip-install-sec1o036\python-ldap\modules\constants.h(7): fatal error c1083: cannot open include file: 'lber.h': no such file or directory
    error: command 'c:\\program files (x86)\\microsoft visual studio 14.0\\vc\\bin\\x86_amd64\\cl.exe' failed with exit status 2

这个报错需要手动安装下whl文件,具体方法为:
先在这个网站下载对应版本的python-ldap的whl文件

然后使用pip命令安装whl,注意文件路径要正确

?
1
2
3
4
5
6
d:\demo\openldap>python -m pip install python_ldap-3.2.0-cp36-cp36m-win_amd64.whl
processing d:\demo\openldap\python_ldap-3.2.0-cp36-cp36m-win_amd64.whl
requirement already satisfied: pyasn1>=0.3.7 in c:\python36\lib\site-packages (from python-ldap==3.2.0) (0.4.2)
requirement already satisfied: pyasn1-modules>=0.1.5 in c:\python36\lib\site-packages (from python-ldap==3.2.0) (0.2.4)
installing collected packages: python-ldap
successfully installed python-ldap-3.2.0

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/db62586b438f

延伸 · 阅读

精彩推荐