mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-26 03:49:31 +08:00
LDAP用户分页读取
This commit is contained in:
parent
ff9e212366
commit
8d03678e71
@ -33,6 +33,7 @@ import xyz.playedu.common.util.ldap.LdapTransformDepartment;
|
||||
import xyz.playedu.common.util.ldap.LdapTransformUser;
|
||||
import xyz.playedu.common.util.ldap.LdapUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -162,7 +163,7 @@ public class LDAPBus {
|
||||
}
|
||||
}
|
||||
|
||||
public void userSync() throws NamingException {
|
||||
public void userSync() throws NamingException, IOException {
|
||||
LdapConfig ldapConfig = appConfigService.ldapConfig();
|
||||
|
||||
List<LdapTransformUser> userList =
|
||||
|
@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.playedu.common.exception.ServiceException;
|
||||
import xyz.playedu.common.util.StringUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import javax.naming.Context;
|
||||
@ -29,8 +30,7 @@ import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.SearchControls;
|
||||
import javax.naming.directory.SearchResult;
|
||||
import javax.naming.ldap.InitialLdapContext;
|
||||
import javax.naming.ldap.LdapContext;
|
||||
import javax.naming.ldap.*;
|
||||
|
||||
@Slf4j
|
||||
public class LdapUtil {
|
||||
@ -75,39 +75,69 @@ public class LdapUtil {
|
||||
}
|
||||
|
||||
public static List<LdapTransformUser> users(
|
||||
String url, String adminUser, String adminPass, String baseDN) throws NamingException {
|
||||
String url, String adminUser, String adminPass, String baseDN) throws NamingException, IOException {
|
||||
LdapContext ldapContext = initContext(url, adminUser, adminPass);
|
||||
|
||||
int pageSize = 1000;
|
||||
List<LdapTransformUser> users = new ArrayList<>();
|
||||
|
||||
SearchControls controls = new SearchControls();
|
||||
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||
controls.setReturningAttributes(USER_RETURN_ATTRS);
|
||||
controls.setReturningObjFlag(true);
|
||||
|
||||
NamingEnumeration<SearchResult> result = null;
|
||||
byte[] cookie = null;
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
result = ldapContext.search(baseDN, USER_OBJECT_CLASS, controls);
|
||||
} catch (NamingException e) {
|
||||
log.error("LDAP用户查询失败", e);
|
||||
} finally {
|
||||
closeContext(ldapContext);
|
||||
if (cookie != null) {
|
||||
ldapContext.setRequestControls(new Control[]{
|
||||
new PagedResultsControl(pageSize, cookie, false),
|
||||
});
|
||||
} else {
|
||||
ldapContext.setRequestControls(new Control[]{
|
||||
new PagedResultsControl(pageSize, false)
|
||||
});
|
||||
}
|
||||
|
||||
if (result == null || !result.hasMoreElements()) {
|
||||
NamingEnumeration<SearchResult> result = ldapContext.search(baseDN, USER_OBJECT_CLASS, controls);
|
||||
while (result.hasMoreElements()) {
|
||||
SearchResult item = result.nextElement();
|
||||
if (item != null) {
|
||||
LdapTransformUser ldapTransformUser = parseTransformUser(item, baseDN);
|
||||
users.add(ldapTransformUser);
|
||||
}
|
||||
}
|
||||
|
||||
cookie = parseCookie(ldapContext.getResponseControls());
|
||||
if (cookie == null || cookie.length == 0) {
|
||||
break;
|
||||
}
|
||||
} catch (NamingException e) {
|
||||
log.error("LDAP用户查询失败", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
closeContext(ldapContext);
|
||||
|
||||
if (users.isEmpty()) {
|
||||
log.info("LDAP服务中没有用户");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<LdapTransformUser> users = new ArrayList<>();
|
||||
while (result.hasMoreElements()) {
|
||||
SearchResult item = result.nextElement();
|
||||
if (item == null) {
|
||||
continue;
|
||||
}
|
||||
LdapTransformUser ldapTransformUser = parseTransformUser(item, baseDN);
|
||||
users.add(ldapTransformUser);
|
||||
return users;
|
||||
}
|
||||
|
||||
return users;
|
||||
private static byte[] parseCookie(Control[] controls) throws NamingException {
|
||||
if (controls != null) {
|
||||
for (Control control : controls) {
|
||||
if (control instanceof PagedResultsResponseControl) {
|
||||
return ((PagedResultsResponseControl) control).getCookie();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<LdapTransformDepartment> departments(
|
||||
|
Loading…
x
Reference in New Issue
Block a user