You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.9 KiB
104 lines
3.9 KiB
package com.spring.common.utils;
|
|
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import javax.naming.NamingEnumeration;
|
|
import javax.naming.NamingException;
|
|
import javax.naming.PartialResultException;
|
|
import javax.naming.directory.Attributes;
|
|
import javax.naming.directory.SearchControls;
|
|
import javax.naming.directory.SearchResult;
|
|
import javax.naming.ldap.Control;
|
|
import javax.naming.ldap.LdapContext;
|
|
import javax.naming.ldap.PagedResultsControl;
|
|
import javax.naming.ldap.PagedResultsResponseControl;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
/**
|
|
*
|
|
* @ClassName: LdapReadUtils
|
|
* @Description: 读取数据
|
|
* @author: LR
|
|
* @date: 2025年9月5日 下午12:04:30
|
|
* @Copyright:
|
|
*/
|
|
public class LdapReadUtils {
|
|
private static final Logger logger = LoggerFactory.getLogger(LdapReadUtils.class);
|
|
|
|
/**
|
|
*
|
|
* @Title: getAllLdapAccount
|
|
* @Description: 查询所有域控行号 通过分页查询来做
|
|
* @author: LR
|
|
* @date 2025年9月5日 下午12:07:09
|
|
* @return: Map<String,String>
|
|
*/
|
|
public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase, RedisTemplate<String, Object> redisTemplate) throws NamingException, IOException {
|
|
byte[] cookie = null;
|
|
// 设置返回所有属性
|
|
SearchControls controls = new SearchControls();
|
|
controls.setReturningAttributes(new String[] {
|
|
"sAMAccountName", "displayName", "distinguishedName", "manager"
|
|
});
|
|
controls.setSearchScope(SearchControls.SUBTREE_SCOPE); //
|
|
|
|
String nameFilter = "(&(objectClass=user)(objectCategory=person)(!(objectClass=computer)))";
|
|
Map<String, String> ldapAccountMap = new HashMap<>();
|
|
int pageNums = 1;
|
|
int countNums = 0;
|
|
//循环执行
|
|
do{
|
|
ctx.setRequestControls(new Control[]
|
|
{new PagedResultsControl(100, cookie, true)});
|
|
NamingEnumeration<SearchResult> results = null;
|
|
try{
|
|
results = ctx.search(ldapBase, nameFilter, controls);
|
|
countNums = 1;
|
|
//获取数据 读取人员组织架构+域控账号
|
|
while (results.hasMore()) {
|
|
SearchResult result = results.next();
|
|
Attributes attrs = result.getAttributes();
|
|
String distinguishedName = attrs.get("distinguishedName").get().toString();
|
|
String sAMAccountName = attrs.get("sAMAccountName").get().toString();
|
|
ldapAccountMap.put(distinguishedName, sAMAccountName);
|
|
//logger.warn("分页查询第:{}条, distinguishedName: {}, sAMAccountName: {}", countNums, distinguishedName, sAMAccountName);
|
|
redisTemplate.opsForHash().put("ldapAccount", distinguishedName, sAMAccountName);
|
|
countNums++;
|
|
}
|
|
}catch(PartialResultException pre){
|
|
//logger.warn("Search results: {}", pre.getMessage());
|
|
throw new PartialResultException(pre.getMessage());
|
|
}finally{
|
|
ctx.setRequestControls(null);
|
|
}
|
|
pageNums++;
|
|
// 处理服务器返回的分页响应
|
|
Control[] controlsResp = ctx.getResponseControls();
|
|
if (controlsResp != null) {
|
|
for (Control control : controlsResp) {
|
|
if (control instanceof PagedResultsResponseControl) {
|
|
PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
|
|
cookie = prrc.getCookie();
|
|
}
|
|
}
|
|
} else {
|
|
cookie = null;
|
|
}
|
|
//logger.warn("分页查询结束,分码:{}, 数据统计量:{}", pageNums, countNums);
|
|
}while(cookie != null && cookie.length > 0);
|
|
logger.warn("Query finished, count={}", ldapAccountMap.size());
|
|
//返回结果数据
|
|
return ldapAccountMap;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|