【发布时间】:2021-04-28 07:07:33
【问题描述】:
这个问题无疑是this one 的重复,但它没有答案。此外,您可能倾向于认为this answer 解决了问题,但事实并非如此。 (我怀疑那些回答的人实际上是否尝试过 Active Directory,他们可能尝试过其他一些 LDAP 实现)。
目标是检索 RootDSE, 无需与用户进行身份验证,即所谓的匿名连接。 (是的,AD 支持这个)
这是我尝试过的(使用 Java 8):
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class MyMain {
public static void main(String[] args) throws NamingException {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldap://myadsrv.mynet.net:389");
props.put(Context.SECURITY_AUTHENTICATION, "none"); // shouldn't be necessary, but just to make a point of it
LdapContext ctx = new InitialLdapContext(props, null);
// Force a bind on the connection
ctx.reconnect(ctx.getConnectControls());
// Get the RootDSE
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.OBJECT_SCOPE);
searchControls.setReturningAttributes(new String[]{"namingContexts"});
NamingEnumeration<SearchResult> result = ctx.search("", "objectClass=*", searchControls);
}
}
无论我做什么,我总是会遇到以下异常。在上面的示例中,它将发生在最后一行,即ctx.search(....)。例外:
Exception in thread "main" javax.naming.NamingException:
[LDAP: error code 1 - 000004DC: LdapErr: DSID-0C09072B,
comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580];
remaining name ''
我尝试过其他工具,例如 Apache Directory Studio(开源 LDAP 浏览器,用 Java 编写)和 ldapsearch(来自OpenLDAP suite 的命令行工具),这些工具确实能够检索未经身份验证的 RootDSE。我已经使用 Wireshark 来弄清楚这些工具与我的 JNDI 实现的不同之处,并且可以看到一些差异,但我不太确定我完全理解 Wireshark 输出(并且不知道如何将其转换为 JNDI,因为后者是非常高级的,但 Wireshark 当然是非常低级的)。但至少证明了这是可能的。
【问题讨论】:
标签: java active-directory ldap jndi