试图让我的 Django 身份验证与 LDAP 服务器一起工作,但我无法登录,我已经设法使用 forumsys 使用AUTH_LDAP_USER_DN_TEMPLATE 测试 LDAP 服务器来做到这一点,但是将两者都添加到 LDAPSearchUnion (我将在我的实际项目中需要)它只显示“用户名/密码不正确”(登录失败时我的消息)
这是我的settings.py:
AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com"
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearchUnion(
LDAPSearch("ou=mathematicians,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
LDAPSearch("ou=scientists,dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)"),
)
AUTH_LDAP_USER_ATTR_MAP = {
'first_name': 'givenName',
'last_name': 'sn',
'email': 'mail',
}
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTHENTICATION_BACKENDS = (
'django_auth_ldap.backend.LDAPBackend',
)
视图.py:
def login_page(request):
if request.user.is_authenticated:
return redirect("list")
context = {
'form': LoginForm,
}
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('list')
else:
messages.error(request, 'Wrong username or password')
return redirect("login")
return render(request, 'login.html', context)
有谁知道我做错了什么?
编辑
我设法通过使用它来做到这一点:
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=dc=example,dc=com",
ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
但想法是添加一个 OU(例如科学家),这样只有科学家才能登录应用程序。