我有一段Java代码可以简单搜索Active Directory。代码的功能如预期的那样在使用生产AD时使用,但是当在测试AD上使用相同的代码时,不会返回任何结果(也不会引发异常或错误)。LDAP不返回任何结果
在我的机器上使用AD浏览器时,我可以浏览和搜索测试AD并查找我正在查找的结果。
AD允许读取所有人的权限,因此它不是权限问题。
有谁知道什么可能导致它不会返回任何结果到我的Java客户端,但对我的浏览器呢?
Java代码:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, Constants.LDAPURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.REFERRAL, "follow");
DirContext dctx = new InitialDirContext(env);
String base = Constants.LDAPQUERYLOCATION;
SearchControls sc = new SearchControls();
String[] attributeFilter = {"cn", "sAMAccountName", "sn", "distinguishedName"};
sc.setReturningAttributes(attributeFilter);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(objectClass=User)(sn=smith))";
NamingEnumeration results = dctx.search(base, filter, sc);
if(!results.hasMore()){
log.debug("No results found");
}
while (results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
Attribute attr = attrs.get("cn");
log.debug("cn: "+attr.get());
attr = attrs.get("sn");
log.debug("sn: "+attr.get());
attr = attrs.get("distinguishedName");
log.debug("dn: "+attr.get());
}
dctx.close();
我没有AD的控制,所以我真的不能提供有关其设置的许多信息。
您可以在测试和生产中提供以下参数:Constants.LDAPURL和Constants.LDAPQUERYLOCATION吗? – JPBlanc