【发布时间】:2018-11-20 06:43:36
【问题描述】:
我正在尝试使用简单的 LDAP 身份验证创建一个 django(v2.1.3) Web 应用程序。代码运行,我不完全知道它为什么不工作。它似乎没有向我连接到的 LDAP 后端验证用户信息。当我填写表格时,当我知道用户在测试服务器上时,它总是会返回“非活动用户”。我想要的只是让它认识到它是一个“有效用户”
我正在针对此处找到的 LDAP 测试服务器运行它http://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
这是我在项目中所做的更改:
settings.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',
]
views.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:
当用户返回无用户时,我使用了记录器来获取此错误
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)'},)
【问题讨论】:
-
我看到您使用的是“电子邮件”,您真的是在传递电子邮件:“boyle@foo.com”还是只是“boyle”?在 Django 中启用调试日志记录可能会有所帮助:django-auth-ldap.readthedocs.io/en/latest/logging.html
-
authenticate(username=request.POST.get('email'), password=request.POST.get('password'))这会将电子邮件变量设置为我相信的 uid,我会尝试调试它.. 谢谢 -
发现调试错误,不知道怎么回事,请检查最新编辑。
-
看起来它正在尝试使用 TLS 连接到 LDAP 服务器。不知道为什么,因为您指定了 ldap://.. 您可以尝试在设置中设置:
AUTH_LDAP_START_TLS = False。查看那个站点,他们没有在他们的 ldap 服务器上配置 TLS。 -
我将它设置为 false 并且它起作用了!谢谢你!编辑您的答案以添加该内容并奖励您赏金。