LDAP认证一次仅可用于一个OU

问题描述 投票:0回答:1

我正在尝试使用Java使用开放式LDAP进行用户身份验证。问题在于,它一次仅对一个OU(例如,某人)进行身份验证。如果用户在另一个OU(例如零售)中,则系统给出的凭据无效。

以下代码段可对ou people下的任何用户进行完美身份验证,但对ou retail下的任何用户均无效:

String username = "tst";
String password = "tst@123";
String dn = "cn=" + username + ",ou=people,dc=partners,dc=abcd,dc=in";
String ldapURL = "ldap://myldapserver:389";

Hashtable<String, String> environment = new Hashtable<String, String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, ldapURL);
environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, dn);
environment.put(Context.SECURITY_CREDENTIALS, password);

如何使此代码可用于验证可能属于任何OU的用户?

java active-directory ldap openldap ou
1个回答
0
投票

这是因为您试图通过显式添加Users OU来提供专有名称:

String dn = "cn=" + username + ",ou=people,dc=partners,dc=abcd,dc=in";

但是您不需要这样做。 AD仅接受用户名。因此,您可以删除该行,然后将username传递为Context.SECURITY_PRINCIPAL

environment.put(Context.SECURITY_PRINCIPAL, username);

使用Simple Authentication时可接受的格式请参见此处。

© www.soinside.com 2019 - 2024. All rights reserved.