gpt4 book ai didi

java - 在没有用户名和密码的情况下连接到 Java 中的 LDAP Active Directory

转载 作者:行者123 更新时间:2023-11-30 06:51:18 26 4
gpt4 key购买 nike

我有一个用 C# 编写的简单应用程序。该应用程序可在 Windows 机器上运行,并且用户帐户已登录到 Active Directory。

在该应用程序中,我从 LDAP Active Directory 中检索了一些信息。

在 C# 中,我以这种方式连接 LDAP:

DirectoryEntry de = new DirectoryEntry("LDAP://OU=places,DC=system,DC=org");

接下来我检索所需的数据(例如):

DirectorySearcher searcher = new DirectorySearcher(de);
searcher.Filter = "(telephone=111222333)";
SearchResultCollection resultCollection = searcher.FindAll();

正如您在该应用程序中看到的,我不需要使用用户名和密码登录 LDAP,但我只需要输入正确的路径:

DirectoryEntry 的构造函数如下所示:

public DirectoryEntry(
string path
)

当然有一个带有用户名和密码的构造函数,但正如我所说我不需要使用它:

public DirectoryEntry(
string path,
string username,
string password
)

我试图在 Java 中检索相同的信息并且我使用了类似的代码:

Hashtable<String, String> env = new Hashtable<String, String>();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://ad.host:389");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "password");

DirContext ldap = new InitialDirContext(env);

接下来我检索所需的数据(例如):

SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration tel = ldap.search("OU=places,DC=system,DC=org", "(telephone=111222333)", searchControls);

但是有一个区别。如您所见,如果我想连接到该 LDAP Active Directory,我需要在 Java 中输入用户名和密码。

如果我不输入用户名和密码,我会看到以下错误:

Exception in thread "main" javax.naming.NamingException:[LDAP: error code 1 - 000004DC: LdapErr: DSID-0C0906E8, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1;] remaining name 'OU=places,DC=system,DC=org'

我的问题是,是否可以在 Java 中从同一台机器登录到同一个 LDAP Active Directory 而无需输入用户名和密码?

是否有像在 C# 中那样在 Java 中执行此操作的等效项(无需输入用户名和密码)?

最佳答案

如果使用 env.put(Context.SECURITY_AUTHENTICATION, "none"); 不起作用,那么您的 AD 环境可能不支持匿名身份验证,这是可以理解的。默认情况下禁用匿名身份验证。

当您在 C# 中使用 new DirectoryEntry(...); 时,您似乎没有使用任何凭据,但它实际上使用的是当前登录用户的凭据。所以它是在借用您自己的凭据来调用 AD。

Java 不会那样做。事实上,从我刚才所做的简短谷歌搜索来看,如果你想这样做的话,似乎很难做到这一点。

这里有一个关于如何做到这一点的问题:Query Active Directory in Java using a logged on user on windows

那里的评论提供了一些产品供研究。

但如果您不想使用任何可能使事情复杂化的第三方产品,您只需提供用户名和密码即可。

如果你想在 C# 中测试匿名身份验证,你可以使用这样的东西:

new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous)

关于java - 在没有用户名和密码的情况下连接到 Java 中的 LDAP Active Directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com