|
|
|
@ -17,6 +17,7 @@ import javax.naming.ldap.PagedResultsResponseControl; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
@ -37,7 +38,7 @@ public class LdapReadUtils { |
|
|
|
* @date 2025年9月5日 下午12:07:09 |
|
|
|
* @return: Map<String,String> |
|
|
|
*/ |
|
|
|
public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase) throws NamingException, IOException { |
|
|
|
public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase, RedisTemplate<String, Object> redisTemplate) throws NamingException, IOException { |
|
|
|
byte[] cookie = null; |
|
|
|
// 设置返回所有属性 |
|
|
|
SearchControls controls = new SearchControls(); |
|
|
|
@ -48,6 +49,8 @@ public class LdapReadUtils { |
|
|
|
|
|
|
|
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[] |
|
|
|
@ -55,21 +58,25 @@ public class LdapReadUtils { |
|
|
|
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(); |
|
|
|
//logger.info("distinguishedName:"+distinguishedName); |
|
|
|
//logger.info("sAMAccountName:"+sAMAccountName); |
|
|
|
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()); |
|
|
|
//logger.warn("Search results: {}", pre.getMessage()); |
|
|
|
throw new PartialResultException(pre.getMessage()); |
|
|
|
}finally{ |
|
|
|
ctx.setRequestControls(null); |
|
|
|
} |
|
|
|
} |
|
|
|
pageNums++; |
|
|
|
// 处理服务器返回的分页响应 |
|
|
|
Control[] controlsResp = ctx.getResponseControls(); |
|
|
|
if (controlsResp != null) { |
|
|
|
@ -82,10 +89,9 @@ public class LdapReadUtils { |
|
|
|
} else { |
|
|
|
cookie = null; |
|
|
|
} |
|
|
|
|
|
|
|
//logger.warn("分页查询结束,分码:{}, 数据统计量:{}", pageNums, countNums); |
|
|
|
}while(cookie != null && cookie.length > 0); |
|
|
|
|
|
|
|
|
|
|
|
logger.warn("Query finished, count={}", ldapAccountMap.size()); |
|
|
|
//返回结果数据 |
|
|
|
return ldapAccountMap; |
|
|
|
} |
|
|
|
|