解决LDAP域同步 只同步一千个用户问题

原生LdapContext 默认只加载一千条数据,无法满足几千条,需要自行调整代码

  1. 第一个 ctx.setRequestControls(new Control[] { new
    PagedResultsControl(maxResults, Control.NONCRITICAL) });
    需要设置为NONCRITICAL,才能进行自动翻页,否则不行 第二个则正常设置Control.CRITICAL即可

  2. 利用分页缓存读取的方法,去进行循环判断是否存在多余数据,没有之后写入自己的功能代码块

    public boolean pullData(SysLdapConfig config,String searchBase,String searchFilter){
        boolean ret = false;
        LdapContext  ctx = geLdapContext(config);
        if (ctx == null) {
            return false;
        }
        try {
            SearchControls searchCtls = new SearchControls();
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String returnedAtts[] = {"sAMAccountName", "displayName", "title",
                    "mail", "telephonenumber", "facsimiletelephonenumber",
                    "homephone", "mobile", "distinguishedName","userPrincipalName"};
//            searchCtls.setReturningAttributes(returnedAtts);
            byte[] cookie = null;
            // 设置最大返回结果数量
            int maxResults = 1000;

            ctx.setRequestControls(new Control[] { new PagedResultsControl(maxResults, Control.NONCRITICAL) });

            int totalResults = 0;
            int rows = 0;
            String initialPassword = sysParmService.getValue("InitialPassword");
            List<SysUser> list = new ArrayList<>();
            do{
                NamingEnumeration<SearchResult> answer = ctx.search(searchBase, searchFilter, searchCtls);
                while (answer!=null && answer.hasMore()) {
                    SearchResult sr = (SearchResult) answer.next();
                    //String match = dn.split("CN=")[1].split(",")[0];//返回格式一般是CN=ptyh,OU=专卖
                    Attributes Attrs = sr.getAttributes();
                    if (Attrs != null) {
                        try {
                            SysUser user = new SysUser();
                            user.setPassword("");
                            user.setPwdChangeTime(new Date());
                            for (NamingEnumeration ne = Attrs.getAll(); ne.hasMore();) {
                                Attribute Attr = (Attribute) ne.next();
                                String company = null;
                                for (NamingEnumeration e = Attr.getAll(); e.hasMore(); totalResults++) {
                                    company =  e.next().toString();
                                }
                                if("sAMAccountName".equals(Attr.getID())){
                                    user.setAccount(company);
                                }
                                if("displayName".equals(Attr.getID())){
                                    user.setName(company);
                                }
                                if("mail".equals(Attr.getID())){
                                    user.setEmail(company);
                                }
                                if("telephonenumber".equals(Attr.getID())){
                                    user.setTelephone(company);
                                }
                                if("facsimiletelephonenumber".equals(Attr.getID())){
                                    user.setFax(company);
                                }
                                if("mobile".equals(Attr.getID())){
                                    user.setCellphone(company);
                                }
                                if ("userPrincipalName".equals(Attr.getID())) {
                                    user.setDomainName(company.substring(company.lastIndexOf("@")+1,company.length()));
                                }
                                if("distinguishedName".equals(Attr.getID()) && !company.isEmpty() && company.contains("OU=")){
                                    //格式化用户机构信息
                                    List<String> distinguishedNames = new ArrayList<>();
                                    String[] strs = company.substring(company.indexOf("OU=") + 3, company.length()).split("OU=");
                                    for (int i = strs.length-1; i>=0 ;i--) {
                                        distinguishedNames.add(strs[i].split(",")[0]);
                                    }
                                    user.setDistinguishedNames(distinguishedNames);
                                }
                            }
                            user.setType(3);
                            list.add(user);
                        } catch (NamingException e) {
                            System.err.println("Throw Exception : " + e);
                        }
                    }
                }

                //读取cookie,判断是否有未读取完
                Control[] controls = ctx.getResponseControls();
                if (controls != null) {
                    for (int i = 0; i < controls.length; i++) {
                        if (controls[i] instanceof PagedResultsResponseControl) {
                            PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                            cookie = prrc.getCookie();
                        }
                    }
                }

                // 将cookie提供给LdapContext,让它在接下来的查询中进行换页
                ctx.setRequestControls(new Control[]{new PagedResultsControl(maxResults, cookie, Control.CRITICAL)});

            }while (cookie!=null);

            ret = syncUsers(list,0);
        } catch (javax.naming.AuthenticationException e) {
            log.error("认证失败");
            logger.error("同步用户失败{}",e.getMessage());
        } catch (Exception e) {
            log.error("认证出错:", e);
        } finally {
            if(ret){
                try {
                    ctx.close();
                    ctx = null;
                } catch (NamingException ex) {
                    log.error("关闭 LDAP 连接失败", ex);
                }
            }
        }
        return ret;
    }
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳关三戏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值