// LDAP连线相关设定
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=maxkit,dc=com,dc=tw");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.REFERRAL, "ignore");
env.put(Context.PROVIDER_URL, LDAP_URL);
LdapContext ctx = new InitialLdapContext(env, null);
// 设定分页相关资讯
int pageSize = 1000; //设定LDAP每次分页所取的资料笔数
byte[] cookie = null;
ctx.setRequestControls(new Control[]{new PagedResultsControl(
pageSize, Control.CRITICAL)});
do {
// 设定LDAP 人员查询条件
String searchFilter = "(objectClass=organizationalPerson)";
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 进行LDAP 资料查询与资料处理
NamingEnumeration<SearchResult> results = ctx.search(
"dc=maxkit,dc=com,dc=tw", searchFilter, searchCtls);
while (results != null && results.hasMore()) {
// ...
// 这边执行LDAP 查出来的人员相关处理逻辑
// ...
}
//================================================ ==
// 换页处理开始
//================================================ ==
// 此分页资料处理完毕,底下先取出cookie,
// 如果cookie不为null,则表示还有下一页的资料
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc =
(PagedResultsResponseControl) controls[i];
cookie = prrc.getCookie();
}
}
}
// 将cookie资讯提供给InitialLdapContext,让它在接下来的查询中进行换页
ctx.setRequestControls(new Control[]{new PagedResultsControl(
pageSize, cookie, Control.CRITICAL)});
//================================================ ==
// 换页处理结束
//================================================ ==
} while (cookie != null);
ctx.close();