List the root contexts in LDAP

怎甘沉沦 提交于 2019-12-11 03:07:46

问题


I would like to list or search the root context(s) in a LDAP tree. I use Apache Directory Server and Java:

    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());
    }

I can list the sub nodes of ou=system. But I cannot list the sub nodes of the actual root node. I would like to have this list just like Apache Directory Studio can: alt text http://lesc.se/stackoverflow/ldap_root_contexts.png


回答1:


The base DNs can be obtained from the namingContexts attribute of the root node (RootDSE). The code should look like this:

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);
}


来源:https://stackoverflow.com/questions/2616474/list-the-root-contexts-in-ldap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!