Попытка вытащить список пользователей из больших групп AD через Java - но получить только 1500 назад - как я могу получить всех пользователей?
// Step1 method - Pulling ADGroups from Active Directory
private static void getADGroups() {
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://");
env.put(Context.SECURITY_PRINCIPAL, "xxxx");
env.put(Context.SECURITY_CREDENTIALS, "1233");
env.put(Context.REFERRAL, "follow");
LdapContext ctx = null;
try {
ctx = new InitialLdapContext(env, null);
// Activate paged results
int pageSize = 10000;
byte[] cookie = null;
ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
int total;
do {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = { "cn" };
searchControls.setReturningAttributes(attrIDs);
String searchBase = "OU=Groups,DC=cof,DC=ds,DC=com";
String searchFilter = "CN=*Ranger*";
/* perform the search */
NamingEnumeration results = ctx.search(searchBase, searchFilter, searchControls);
/* for each entry print out name + all attrs and values */
int count = 0;
while (results != null && results.hasMore()) {
SearchResult entry = (SearchResult) results.next();
//System.out.println(count + ")" + entry.getName());
count = count + 1;
String gname = entry.getName();
//System.out.println("gname before split " + gname);
String[] gnames = gname.split(",");
gname = gnames[0];
//System.out.println("gname after split - 1 " + gname);
gname = gname.substring(3);
//System.out.println("gname after split - 2 " + gname);
groups.add(gname);
}
//System.out.println("count : " + count);
// Examine the paged results control response
Control[] controls = ctx.getResponseControls();
//System.out.println("controls-size : " + controls.length);
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
total = prrc.getResultSize();
//System.out.println("total : " + total);
if (total != 0) {
//System.out.println("*****************
cookie = prrc.getCookie();
//System.out.println("cookie : " + cookie);
}
}
} else {
System.out.println("No controls were sent from the server");
}
// Re-activate paged results
ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
} while (cookie != null);
} catch (NamingException e) {
System.out.println("PagedSearch failed." + e.getMessage());
e.printStackTrace();
} catch (IOException ie) {
System.out.println("PagedSearch failed." + ie.getMessage());
ie.printStackTrace();
} finally {
try {
ctx.close();
} catch (NamingException e) {
System.out.println("PagedSearch failed (error occured in closing context)." + e.getMessage());
e.printStackTrace();
}
}
}
// Step2 method - to pull users from ADgroups that we got for above
private static void getGroupMembers(String groupName) {
searchBase = "Ou=users";
String returnedAtts[] = { "member" };
searchControls.setReturningAttributes(returnedAtts);
searchFilter = String.format("(cn=%s)", groupName);
// System.out.println(searchFilter);
getSearchResult();
filterSearchResultsForGroupMembers(groupName);
} // end of method.
`