1

我正在尝试从 AD 服务器获取所有用户。AD 服务器中有 7000 个用户,但是当我运行我的 java 代码时,它只返回 1000 个用户名。是否有任何限制,它一次只能获取最多 1000 个用户?谁能告诉我如何一次获取所有 7000 个用户。

这是Java代码-

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;


public class FetchAllUsers {


public static void main(String args[])
{
  String username = "username";

  String password = "password";

  String ldapURL = "url";

  String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";

  String securityAuthentication = "simple";

  fetchUserList(username,password,ldapURL,contextFactory,securityAuthentication);
}


public static void fetchUserList(String username,String password,String ldapURL,String contextFactory,String securityAuthentication) {

  Hashtable<String, String> env = new Hashtable<String, String>();

  env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);

  // set security credentials
  env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
  env.put(Context.SECURITY_PRINCIPAL, username);
  env.put(Context.SECURITY_CREDENTIALS, password);

  // connect to my domain controller
  env.put(Context.PROVIDER_URL, ldapURL);

  try {

    List<String> usersList = new ArrayList<String>();

    LdapContext ctx = new InitialLdapContext(env, null);

    SearchControls searchCtls = new SearchControls();

    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    // specify the LDAP search filter
    String searchFilter = "(&(objectCategory=user))";

 // Specify the Base for the search
    String searchBase = "DC=domain,DC=com";

    // initialize counter to total the results
    int totalResults = 0;

    // Search for objects using the filter
    NamingEnumeration<SearchResult> fetchData = ctx.search(searchBase, searchFilter, searchCtls);

    // Loop through the search results
    while (fetchData.hasMoreElements()) {
      SearchResult sr = (SearchResult) fetchData.next();
      totalResults++;

        String names[] = sr.getName().split(",");
        String name[] = names[0].split("=");
        usersList.add(name[1]);

    }
    System.out.println("Total number of users in AD server : " + totalResults);
    System.out.println(usersList);

  } catch (NamingException e) {
    e.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
 }
4

0 回答 0