我有一段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