devops--django+ldap
2024-08-30 07:09:46
首先,什么是Ldap,官方解释
轻型目录访问协议(英文:Lightweight Directory Access Protocol,缩写:LDAP,/ˈɛldæp/)是一个开放的,中立的,工业标准的应用协议,通过ip协议提供访问控制和维护分布式信息的目录信息。
ldap能做什么?为什么要用ldap
LDAP的一个常用用途是单点登录,用户可以在多个服务中使用同一个密码,通常用于公司内部网站的登录中(这样他们可以在公司计算机上登录一次,便可以自动在公司内部网上登录)。
话不多说,上代码解释
# settings.py import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, PosixGroupType try:
import ConfigParser as conf
except ImportError as e:
import configparser as conf config = conf.ConfigParser()
config.read(os.path.join(BASE_DIR, 'conf/config.ini')) if config.get('ldap', 'enable') == 'true':
ldap_filter = config.get('ldap', 'filter')
if ldap_filter == "OpenLDAP":
ldap_filter = '(cn=%(user)s)'
else:
ldap_filter = '(sAMAccountName=%(user)s)' AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend', # 首先通过ldap认证,认证不通过走登录接口验证,ldap用户第一次登录,检测到数据库没有该用户,会自动创建一份用户信息到对应user表
'django.contrib.auth.backends.ModelBackend',
) AUTH_LDAP_SERVER_URI = "ldap://{server}:{port}".format(server=config.get('ldap', 'server'),port=config.get('ldap', 'port'))
AUTH_LDAP_BIND_DN = "{bind_dn}".format(bind_dn=config.get('ldap', 'bind_dn'))
AUTH_LDAP_BIND_PASSWORD = "{password}".format(password=config.get('ldap', 'bind_password'))
AUTH_LDAP_USER_SEARCH = LDAPSearch("{search_dn}".format(search_dn=config.get('ldap', 'search_dn')), ldap.SCOPE_SUBTREE, ldap_filter)
AUTH_LDAP_GROUP_TYPE = PosixGroupType(name_attr='cn')
AUTH_LDAP_USER_ATTR_MAP = {
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True if config.get('inception', 'enable') == 'true':
INCEPTION_CONFIG = {
"host":config.get('inception', 'host'),
"port":config.get('inception', 'port'),
"backup_host":config.get('inception', 'backup_host'),
"backup_passwd":config.get('inception', 'backup_passwd'),
"backup_user":config.get('inception', 'backup_user'),
"backup_port":config.get('inception', 'backup_port')
}
# config.ini [ldap]
enable = true
server = xxx.xxx.xxx.xxx # ldap服务器ip
port = 389 # 端口默认389
bind_dn = cn=账号,dc=域名,dc=域名 # 账号必须是ldap管理员账号
bind_password = 密码 # 这些都问公司运维部门要
search_dn = ou=People,dc=域名,dc=域名
filter = OpenLDAP
view.py
用户表可以直接重写django-admin自带的user表,其内置了auth的验证方法
from django.contrib import auth # 登录
def login(request):
if request.session.get('username') is not None:
return HttpResponseRedirect('/',{"user":request.user})
else:
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username,password=password) # 认证方法,验证用户名密码都正确返回该用户对象
if user and user.is_active:
auth.login(request,user)
request.session['username'] = username
return HttpResponseRedirect('/',{"user":request.user})
else:
if request.method == "POST":
return render(request,'login.html',{"login_error_info":"用户名或者密码错误","username":username},)
else:
return render(request,'login.html') # 退出登录
def logout(request):
auth.logout(request) # 登出,清楚session
return HttpResponseRedirect('/login')
创建auth_user中的用户通过
python manage.py create superuser
最新文章
- Android开发案例 - 图库
- web应用程序测试方法和测试技术详述
- 个性化EDM数据营销的三大提醒
- Android Design Support Library——TabLayout
- vc中openGL的安装
- CentOs5.2中PHP的升级
- [HDU 4821] String (字符串哈希)
- 广州项目实施步骤III_练习使用Keepalive保证HaProxy的高可用性
- The Name/Origin of Country names
- ubuntu12.04samba服务器配置,亲测可用(转)
- windows身份验证,那么sqlserver的连接字符串的
- zrender源码分析1:总体结构
- TestFlight使用方法
- TensorFlow实战之实现AlexNet经典卷积神经网络
- 小项目一---Python日志分析
- kubernetes系列之ConfigMap使用方式
- redis高可用(哨兵机制)
- W3School 学习笔记
- jqgrid 对编辑行填写的内容做格式验证
- linux如何通过脚本来修改用户的密码?脚本自动化修改用户密码?