原生LdapContext 默认只加载一千条数据,无法满足几千条,需要自行调整代码
-
第一个 ctx.setRequestControls(new Control[] { new
PagedResultsControl(maxResults, Control.NONCRITICAL) });
需要设置为NONCRITICAL,才能进行自动翻页,否则不行 第二个则正常设置Control.CRITICAL即可 -
利用分页缓存读取的方法,去进行循环判断是否存在多余数据,没有之后写入自己的功能代码块
public boolean pullData(SysLdapConfig config,String searchBase,String searchFilter){
boolean ret = false;
LdapContext ctx = geLdapContext(config);
if (ctx == null) {
return false;
}
try {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String returnedAtts[] = {"sAMAccountName", "displayName", "title",
"mail", "telephonenumber", "facsimiletelephonenumber",
"homephone", "mobile", "distinguishedName","userPrincipalName"};
// searchCtls.setReturningAttributes(returnedAtts);
byte[] cookie = null;
// 设置最大返回结果数量
int maxResults = 1000;
ctx.setRequestControls(new Control[] { new PagedResultsControl(maxResults, Control.NONCRITICAL) });
int totalResults = 0;
int rows = 0;
String initialPassword = sysParmService.getValue("InitialPassword");
List<SysUser> list = new ArrayList<>();
do{
NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter, searchCtls);
while (answer!=null && answer.hasMore()) {
SearchResult sr = (SearchResult) answer.next();
//String match = dn.split("CN=")[1].split(",")[0];//返回格式一般是CN=ptyh,OU=专卖
Attributes Attrs = sr.getAttributes();
if (Attrs != null) {
try {
SysUser user = new SysUser();
user.setPassword("");
user.setPwdChangeTime(new Date());
for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
Attribute Attr = (Attribute) ne.next();
String company = null;
for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++) {
company = e.next().toString();
}
if("sAMAccountName".equals(Attr.getID())){
user.setAccount(company);
}
if("displayName".equals(Attr.getID())){
user.setName(company);
}
if("mail".equals(Attr.getID())){
user.setEmail(company);
}
if("telephonenumber".equals(Attr.getID())){
user.setTelephone(company);
}
if("facsimiletelephonenumber".equals(Attr.getID())){
user.setFax(company);
}
if("mobile".equals(Attr.getID())){
user.setCellphone(company);
}
if ("userPrincipalName".equals(Attr.getID())) {
user.setDomainName(company.substring(company.lastIndexOf("@")+1,company.length()));
}
if("distinguishedName".equals(Attr.getID()) && !company.isEmpty() && company.contains("OU=")){
//格式化用户机构信息
List<String> distinguishedNames = new ArrayList<>();
String[] strs = company.substring(company.indexOf("OU=") + 3, company.length()).split("OU=");
for (int i = strs.length-1; i>=0 ;i--) {
distinguishedNames.add(strs[i].split(",")[0]);
}
user.setDistinguishedNames(distinguishedNames);
}
}
user.setType(3);
list.add(user);
} catch (NamingException e) {
System.err.println("Throw Exception : " + e);
}
}
}
//读取cookie,判断是否有未读取完
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提供给LdapContext,让它在接下来的查询中进行换页
ctx.setRequestControls(new Control[]{new PagedResultsControl(maxResults, cookie, Control.CRITICAL)});
}while (cookie!=null);
ret = syncUsers(list,0);
} catch (javax.naming.AuthenticationException e) {
log.error("认证失败");
logger.error("同步用户失败{}",e.getMessage());
} catch (Exception e) {
log.error("认证出错:", e);
} finally {
if(ret){
try {
ctx.close();
ctx = null;
} catch (NamingException ex) {
log.error("关闭 LDAP 连接失败", ex);
}
}
}
return ret;
}