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

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
  1. package com.spring.common.utils;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.naming.NamingEnumeration;
  6. import javax.naming.NamingException;
  7. import javax.naming.PartialResultException;
  8. import javax.naming.directory.Attributes;
  9. import javax.naming.directory.SearchControls;
  10. import javax.naming.directory.SearchResult;
  11. import javax.naming.ldap.Control;
  12. import javax.naming.ldap.LdapContext;
  13. import javax.naming.ldap.PagedResultsControl;
  14. import javax.naming.ldap.PagedResultsResponseControl;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.data.redis.core.RedisTemplate;
  18. /**
  19. *
  20. * @ClassName: LdapReadUtils
  21. * @Description: 读取数据
  22. * @author: LR
  23. * @date: 2025年9月5日 下午12:04:30
  24. * @Copyright:
  25. */
  26. public class LdapReadUtils {
  27. private static final Logger logger = LoggerFactory.getLogger(LdapReadUtils.class);
  28. /**
  29. *
  30. * @Title: getAllLdapAccount
  31. * @Description: 查询所有域控行号 通过分页查询来做
  32. * @author: LR
  33. * @date 2025年9月5日 下午12:07:09
  34. * @return: Map<String,String>
  35. */
  36. public static Map<String, String> getAllLdapAccount(LdapContext ctx, String ldapBase, RedisTemplate<String, Object> redisTemplate) throws NamingException, IOException {
  37. byte[] cookie = null;
  38. // 设置返回所有属性
  39. SearchControls controls = new SearchControls();
  40. controls.setReturningAttributes(new String[] {
  41. "sAMAccountName", "displayName", "distinguishedName", "manager"
  42. });
  43. controls.setSearchScope(SearchControls.SUBTREE_SCOPE); //
  44. String nameFilter = "(&(objectClass=user)(objectCategory=person)(!(objectClass=computer)))";
  45. Map<String, String> ldapAccountMap = new HashMap<>();
  46. int pageNums = 1;
  47. int countNums = 0;
  48. //循环执行
  49. do{
  50. ctx.setRequestControls(new Control[]
  51. {new PagedResultsControl(100, cookie, true)});
  52. NamingEnumeration<SearchResult> results = null;
  53. try{
  54. results = ctx.search(ldapBase, nameFilter, controls);
  55. countNums = 1;
  56. //获取数据 读取人员组织架构+域控账号
  57. while (results.hasMore()) {
  58. SearchResult result = results.next();
  59. Attributes attrs = result.getAttributes();
  60. String distinguishedName = attrs.get("distinguishedName").get().toString();
  61. String sAMAccountName = attrs.get("sAMAccountName").get().toString();
  62. ldapAccountMap.put(distinguishedName, sAMAccountName);
  63. //logger.warn("分页查询第:{}条, distinguishedName: {}, sAMAccountName: {}", countNums, distinguishedName, sAMAccountName);
  64. redisTemplate.opsForHash().put("ldapAccount", distinguishedName, sAMAccountName);
  65. countNums++;
  66. }
  67. }catch(PartialResultException pre){
  68. //logger.warn("Search results: {}", pre.getMessage());
  69. throw new PartialResultException(pre.getMessage());
  70. }finally{
  71. ctx.setRequestControls(null);
  72. }
  73. pageNums++;
  74. // 处理服务器返回的分页响应
  75. Control[] controlsResp = ctx.getResponseControls();
  76. if (controlsResp != null) {
  77. for (Control control : controlsResp) {
  78. if (control instanceof PagedResultsResponseControl) {
  79. PagedResultsResponseControl prrc = (PagedResultsResponseControl) control;
  80. cookie = prrc.getCookie();
  81. }
  82. }
  83. } else {
  84. cookie = null;
  85. }
  86. //logger.warn("分页查询结束,分码:{}, 数据统计量:{}", pageNums, countNums);
  87. }while(cookie != null && cookie.length > 0);
  88. logger.warn("Query finished, count={}", ldapAccountMap.size());
  89. //返回结果数据
  90. return ldapAccountMap;
  91. }
  92. }