2011-11-25 192 views
0

我有一段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的控制,所以我真的不能提供有关其设置的许多信息。

+0

您可以在测试和生产中提供以下参数:Constants.LDAPURL和Constants.LDAPQUERYLOCATION吗? – JPBlanc

回答

0

只是想尽自己的网络,该网络使用的OpenLDAP您的代码 - 这是我所知道的是不一样的AD,

我没有结果要么直到我改变了我的filter串到这一点:

String filter = "(&(objectClass=inetOrgPerson)(sn=smith))"; 

我通过使用LDAP浏览器窥探到目录中,得到了inetOrgPerson对象类。这是一个很长的过程,但是您的测试AD可能没有使用与您的生产服务器相同的对象类?

快速Google向我展示了微软的implementation of the LDAP standard was lacking at first, but should now be (more) compliant使用inetOrgPerson--也许你的测试AD运行的是旧版本的问题,而你的prod box是最新最好的?或者反之亦然?

+0

我认为,但都使用相同的ObjectClass – AverageMarcus

+0

他们是否在相同版本的操作系统上运行相同版本的AD? – millhouse

+0

据我所知,是的。 – AverageMarcus