2010-04-11 54 views
2

我想列出或搜索LDAP樹中的根環境。我使用Apache Directory Server和Java:列出LDAP中的根環境

Hashtable<String, String> contextParams = new Hashtable<String, String>(); 
    contextParams.put("java.naming.provider.url", "ldap://localhost:10389"); 
    contextParams.put("java.naming.security.principal", "uid=admin,ou=system"); 
    contextParams.put("java.naming.security.credentials", "secret"); 
    contextParams.put("java.naming.security.authentication", "simple"); 
    contextParams.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); 

    DirContext dirContext = new InitialDirContext(contextParams); 

    NamingEnumeration<NameClassPair> resultList; 

    //Works 
    resultList = dirContext.list("ou=system"); 
    while (resultList.hasMore()) { 
     NameClassPair result = resultList.next(); 
     System.out.println(result.getName()); 
    } 

    //Does not work 
    resultList = dirContext.list(""); 
    while (resultList.hasMore()) { 
     NameClassPair result = resultList.next(); 
     System.out.println(result.getName()); 
    } 

我可以列出ou = system的子節點。但是我無法列出實際根節點的子節點。我想要這個列表就像Apache Directory Studio可以: alt text http://lesc.se/stackoverflow/ldap_root_contexts.png

回答

4

可以從根節點(RootDSE)的namingContexts屬性中獲取基準DN。代碼應如下所示:

Attributes attributes = dirContext.getAttributes("", new String[]{"namingContexts"}); 
Attribute attribute = attributes.get("namingContexts"); 
NamingEnumeration<?> all = attribute.getAll(); 
while(all.hasMore()) 
{ 
    String next = (String)all.next(); 
    System.out.println(next); 
} 
+0

謝謝!這是完美的! – 2010-04-11 10:57:06