mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-07-27 21:09:37 +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.LdapTransformUser;
|
||||||
import xyz.playedu.common.util.ldap.LdapUtil;
|
import xyz.playedu.common.util.ldap.LdapUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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();
|
LdapConfig ldapConfig = appConfigService.ldapConfig();
|
||||||
|
|
||||||
List<LdapTransformUser> userList =
|
List<LdapTransformUser> userList =
|
||||||
|
@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import xyz.playedu.common.exception.ServiceException;
|
import xyz.playedu.common.exception.ServiceException;
|
||||||
import xyz.playedu.common.util.StringUtil;
|
import xyz.playedu.common.util.StringUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import javax.naming.Context;
|
import javax.naming.Context;
|
||||||
@ -29,8 +30,7 @@ import javax.naming.directory.Attribute;
|
|||||||
import javax.naming.directory.Attributes;
|
import javax.naming.directory.Attributes;
|
||||||
import javax.naming.directory.SearchControls;
|
import javax.naming.directory.SearchControls;
|
||||||
import javax.naming.directory.SearchResult;
|
import javax.naming.directory.SearchResult;
|
||||||
import javax.naming.ldap.InitialLdapContext;
|
import javax.naming.ldap.*;
|
||||||
import javax.naming.ldap.LdapContext;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class LdapUtil {
|
public class LdapUtil {
|
||||||
@ -75,39 +75,69 @@ public class LdapUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static List<LdapTransformUser> users(
|
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);
|
LdapContext ldapContext = initContext(url, adminUser, adminPass);
|
||||||
|
|
||||||
|
int pageSize = 1000;
|
||||||
|
List<LdapTransformUser> users = new ArrayList<>();
|
||||||
|
|
||||||
SearchControls controls = new SearchControls();
|
SearchControls controls = new SearchControls();
|
||||||
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
||||||
controls.setReturningAttributes(USER_RETURN_ATTRS);
|
controls.setReturningAttributes(USER_RETURN_ATTRS);
|
||||||
controls.setReturningObjFlag(true);
|
controls.setReturningObjFlag(true);
|
||||||
|
|
||||||
NamingEnumeration<SearchResult> result = null;
|
byte[] cookie = null;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
try {
|
try {
|
||||||
result = ldapContext.search(baseDN, USER_OBJECT_CLASS, controls);
|
if (cookie != null) {
|
||||||
} catch (NamingException e) {
|
ldapContext.setRequestControls(new Control[]{
|
||||||
log.error("LDAP用户查询失败", e);
|
new PagedResultsControl(pageSize, cookie, false),
|
||||||
} finally {
|
});
|
||||||
closeContext(ldapContext);
|
} 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服务中没有用户");
|
log.info("LDAP服务中没有用户");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<LdapTransformUser> users = new ArrayList<>();
|
return users;
|
||||||
while (result.hasMoreElements()) {
|
|
||||||
SearchResult item = result.nextElement();
|
|
||||||
if (item == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
LdapTransformUser ldapTransformUser = parseTransformUser(item, baseDN);
|
|
||||||
users.add(ldapTransformUser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
public static List<LdapTransformDepartment> departments(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user