如何使用JNDI从LDAP服务器获取所有可分辨名称(DNs)的列表?

如何使用JNDI从LDAP服务器获取所有可分辨名称(DNs)的列表?,ldap,Ldap,我希望使用JNDI从LDAP服务器获取所有区分名称DNs的列表。我能够使用以下代码获取基本DN: Hashtable<String,String> env = new Hashtable<String,String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + ldapSe

我希望使用JNDI从LDAP服务器获取所有区分名称DNs的列表。我能够使用以下代码获取基本DN:

Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + ldapServer + ":" + ldapPort);
env.put(Context.REFERRAL, "follow");
if(sslEnabled) {
    env.put("java.naming.ldap.factory.socket", TrustAllSSLSocketFactory.class.getName());
}       
// Create the LDAP context
LdapContext context = new InitialLdapContext(env, null);
String base = "";
String filter = "(objectclass=*)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.OBJECT_SCOPE);

// Search the directory for retrieving namingContexts attribute
// which contains all the base DNs values
NamingEnumeration<SearchResult> results = context.search(base, filter, controls);
List<String> namingContextsList = new ArrayList<String>();

// Process attributes
if(results.hasMore()) {
    Attributes attrs = results.next().getAttributes();
    if (attrs != null) {
        Attribute namingContexts = attrs.get("namingContexts");
        NamingEnumeration enumeration = namingContexts.getAll();
        while(enumeration.hasMore()) { 
            namingContextsList.add((String) enumeration.next());
        }
    }
}
System.out.println(namingContextsList);

请您以类似的方式或其他方式帮助获取所有可能的DNs好吗?

只需将对象范围更改为子树范围即可


您知道,这些都有文档记录。

以下代码对我很有用:请注意,您需要提供执行此操作的凭据,并且属性名为DiscriminatedName

String ldapServer = "192.168.0.11";
String ldapPort = "389";
String principal = "CN=user";
String password = "password";
Hashtable<String,String> environment = new Hashtable<String,String>();
environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
environment.put(Context.PROVIDER_URL, "ldap://" + ldapServer + ":" + ldapPort);

environment.put(Context.SECURITY_AUTHENTICATION, "simple");
environment.put(Context.SECURITY_PRINCIPAL, principal);
environment.put(Context.SECURITY_CREDENTIALS, password);

environment.put(Context.REFERRAL, "follow");
environment.put("com.sun.jndi.ldap.connect.pool", "true");

// Create the LDAP context
LdapContext context = new InitialLdapContext(environment, null);
String baseDN = "DC=domain,DC=com" // Put your base DN here
String filter = "(objectclass=*)";

SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//controls.setSearchScope(SearchControls.ONELEVEL_SCOPE); // Use this for first level DNs only


NamingEnumeration<SearchResult> results = context.search(baseDN, filter, controls);
List<String> searchDNsList = new ArrayList<String>();

try {
    // Process attributes
    while(results.hasMore()) {
        Attributes attrs = results.next().getAttributes();
        if (attrs != null) {
            Attribute distinguisedNames = attrs.get("distinguishedName");
            if(distinguisedNames != null) {
                NamingEnumeration enumeration = distinguisedNames.getAll();
            while(enumeration.hasMore()) {
                String searchDN = (String) enumeration.next();
                searchDNsList.add(searchDN);
            }
        }
    }
}
} catch(Exception ex) {
ex.printStackTrace();
}
System.out.println(searchDNsList);  

以示例代码为例,改变这一点是行不通的。它给出:线程主javax.naming.NameNotFoundException中的异常:[LDAP:错误代码32-没有这样的对象];剩余名称