从AD服务器获取所有用户

时间:2015-12-08 06:59:39

标签: java adserver

我正在尝试从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();
  }
 }
 }

0 个答案:

没有答案