Django Ldap Autentication仅适用于DN_TEMPLATE

时间:2019-03-06 19:26:44

标签: django python-3.x ldap django-auth-ldap

试图使我的Django身份验证与LDAP服务器一起工作,但我无法登录,我已经设法通过forumsys test LDAP server并使用AUTH_LDAP_USER_DN_TEMPLATE进行了此操作,但同时将ou和LDAPSearchUnion都添加了(我在真实项目中需要使用),它仅显示“用户名/密码不正确”(如果登录失败,则会显示我的消息)

这是我的设置。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',
)

Views.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(例如科学家),以便只有科学家才能登录到该应用程序。

0 个答案:

没有答案