我正在尝试使用简单的 LDAP 身份验证创建一个 django(v2.1.3) 网络应用程序。代码运行,我不完全知道为什么它不工作。它似乎没有向我连接的 LDAP 后端验证用户信息。当我填写表格时,当我知道用户在测试服务器上时,它总是会返回“非事件用户”。我想要的只是让它认识到它是一个“有效用户”
我正在针对此处找到的 LDAP 测试服务器运行它 http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
以下是我在项目中所做的更改:
设置.py
import ldap
from django_auth_ldap.config import LDAPSearch
AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com:389"
AUTH_LDAP_CONNECTION_OPTIONS = {
ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch(
"dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
AUTHENTICATION_BACKENDS = [
'django_auth_ldap.backend.LDAPBackend',
]
View .py
from django.contrib.auth import authenticate, login
from django.shortcuts import render
def login_user(request):
email = password = ""
state = ""
if request.POST:
email = request.POST.get('email')
password = request.POST.get('password')
print (email, password)
user = authenticate(username=request.POST.get('email'), password=request.POST.get('password'))
if user is not None:
login(request, user)
state = "Valid account"
else:
state = "Inactive account"
return render(request, 'KPI/auth.html', {'state': state, 'email': email})
auth.html
<html>
<head>
<title>Login</title>
</head>
<body>
{{state}}
<form action="" method="post"> {% csrf_token %}
Email address: <input type="text" name="email" value="{{ email }}" />
Password: <input type="password" name="password" value="" />
<input type="submit" value="Log in" />
</form>
</body>
</html>
编辑:
我知道设置配置是正确的,因为当我运行 ldapsearch -W -h ldap.forumsys.com -p 389 -D "cn=read-only-admin,dc=example,dc=com"- b "dc=example,dc=com"-s sub "uid=boyle" 它将只返回 'boyle' 信息
编辑 2:
当用户返回为 none 时,我使用记录器获取此错误
在验证 tesla 时捕获 LDAPError:CONNECT_ERROR({'desc':'连接错误','信息':'错误:14090086:SSL 例程:ssl3_get_server_certificate:证书验证失败(证书链中的自签名证书)' },)
请您参考如下方法:
看起来您没有为服务器使用正确的主机名。
ldap://ldap.forumsys:389 应该是:ldap://ldap.forumsys.com:389
使用诸如 ldapsearch 之类的 ldap 搜索工具可以帮助验证服务器是否正确响应:
$ ldapsearch -LLL -h ldap.forumsys -p 389 -D 'cn=read-only-admin,dc=example,dc=com'
-w password -b 'ou=mathematicians,dc=example,dc=com' -s sub "(objectclass=*)"
ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
$ ldapsearch -LLL -h ldap.forumsys.com -p 389 -D 'cn=read-only-
admin,dc=example,dc=com' -w password -b 'ou=mathematicians,dc=example,dc=com'
-s sub "(objectclass=*)"
dn: ou=mathematicians,dc=example,dc=com
uniqueMember: uid=euclid,dc=example,dc=com
uniqueMember: uid=riemann,dc=example,dc=com
uniqueMember: uid=euler,dc=example,dc=com
uniqueMember: uid=gauss,dc=example,dc=com
uniqueMember: uid=test,dc=example,dc=com
ou: mathematicians
cn: Mathematicians
objectClass: groupOfUniqueNames
objectClass: top
如果您取回数据,则意味着它正在寻找用户。结果:0 成功仅表示 ldap 服务器能够成功搜索 树。
看起来 Django 模块有两种验证用户的方法。您配置的设置首先尝试查找记录(通过 uid),然后使用找到的 DN 与提供的密码(再次)绑定(bind)。例如,它将搜索 (uid=boyle),然后找到 DN:uid=boyle,dc=example,dc=com。然后它将绑定(bind)到具有 DN 的 LDAP 服务器:uid=boyle,dc=example,dc=com,password 通过登录页面提供。
回应上面的编辑 2:
以下错误表示库正在尝试协商 TLS 连接:
Caught LDAPError while authenticating tesla: CONNECT_ERROR({'desc':
'Connect error', 'info': 'error:14090086:SSL
routines:ssl3_get_server_certificate:certificate verify failed (self
signed certificate in certificate chain)'},)
如果您连接到端口 389 (ldap://) 上的 ldap,则不应协商 TLS,并且可以通过设置将其禁用:
AUTH_LDAP_START_TLS = False
在 settings.py 中。
出于安全原因,最好使用 ldap,并通过 AUTH_LDAP_CONNECTION_OPTIONS 字典配置 TLS 选项。
