How to fetch list of all distinguished names (DNs) from LDAP server using JNDI?

StackOverflow https://stackoverflow.com/questions/22936502

  •  29-06-2023
  •  | 
  •  

Domanda

I wish to fetch the list of all distinguised names (DNs) from LDAP server using JNDI. I am able to fetch the base DN using following code:

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

Could you please help in fetching all the possible DNs in similar manner or other?

È stato utile?

Soluzione 2

Following code works for me: (Note that you need to provide credentials to perform this operation and the attribute name is "distinguishedName")

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

Altri suggerimenti

Just change OBJECT_SCOPE to SUBTREE_SCOPE.

This is all documented, you know.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top