如何在 JAVA 中获取我的 ldapsearch 中的所有 dn

How to get all dn in my ldapsearch in JAVA

在 Linux Shell 中,我使用以下命令获取 LDAP 中的所有专有名称 (DN):

ldapsearch -x -b "" -H URL -D uid=administrator,cn=admins,cn=city  -w PASS |grep dn:

我的问题:如何像我使用上述命令那样获取 Java 中的所有 DN?

您可以使用Java Naming and Directory Interface (JNDI)

这是一个从链接教程中获得启发的示例:

import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;

/**
 * Retrieves the DN from the search results
 */
class FullName {
  public static void printSearchEnumeration(NamingEnumeration retEnum) {
    try {
      while (retEnum.hasMore()) {
        SearchResult sr = (SearchResult) retEnum.next();
        System.out.println(">>" + sr.getNameInNamespace());
      }
    } 
    catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=administrator,cn=admins,cn=city");
    env.put(Context.SECURITY_CREDENTIALS, "PASS");

    // Perform search in the entire subtree.
    SearchControls ctl = new SearchControls();
    ctl.setSearchScope(SearchControls.SUBTREE_SCOPE);

    try {
      // Create initial context
      DirContext ctx = new InitialDirContext(env);

      NamingEnumeration answer = ctx.search("", null, ctl);

      // Print the answer
      printSearchEnumeration(answer);

      // Close the context when we're done
      ctx.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}