使用springboot检索LDAP中超过1000条记录

问题描述 投票:0回答:1

我正在使用 ldaptemplate 通过 sring-ldap-core 3.2 从 Active Directory 检索许多记录。 这是我正在使用的代码

    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("employeeType", "PROGRAMMER"));

    List<String> results = new ArrayList<>();
    byte[] cookie = null; // Initialize cookie to null

    do {
        // Create a new LDAP context for each page of results
        LdapContext ldapContext = (LdapContext) ldapTemplate.getContextSource().getReadOnlyContext();
        ldapContext.setRequestControls(new Control[] { new PagedResultsControl(1000, cookie, Control.CRITICAL) });

        // Perform the search with pagination
        ldapTemplate.search("", filter.encode(), new AbstractContextMapper<String>() {
        @Override
        protected String doMapFromContext(DirContextOperations ctx) {
            return ctx.getNameInNamespace();
        }
        }).forEach(results::add);

        // After performing the search, check the response control for the cookie
        Control[] controls = ldapContext.getResponseControls();
        if (controls != null) {
        for (Control control : controls) {
            if (control instanceof PagedResultsResponseControl) {
            cookie = ((PagedResultsResponseControl) control).getCookie();
            }
        }
        }
        ldapContext.close();
} while (cookie != null && cookie.length > 0);

但是控制分页的 cookie 始终为 null,事实上 ldapContext.getResponseControls() 始终为 null,因此分页只返回第一个包含 1000 条记录的查询(结果应该是更多记录)。 我在 ldap 配置中禁用了池化,因为这是建议,但它不起作用。

@Bean
public LdapContextSource ldapContextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.urls"));
contextSource.setBase(env.getRequiredProperty("ldap.base"));
contextSource.setUserDn(env.getRequiredProperty("ldap.username"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
contextSource.setPooled(false);
return contextSource;
}

我正在使用 Microsoft Active Directory,但我不知道还可以尝试什么。大多数情况下,我需要较小的结果(并且我将按修改日期进行过滤),但对于奇怪的情况,我需要大量记录来验证其状态。

spring-boot active-directory ldap-query spring-ldap
1个回答
0
投票

这有效:

AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("employeeType","PROGRAMMER"));

List<String> results = new ArrayList<>();
PagedResultsCookie cookie = null;
boolean hashMore = false;
do {
    Name baseDn = new LdapName("");
    SearchControls searchControls = new SearchControls();
    searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(1000, cookie);
    ldapTemplate.search(baseDn, filter.encode(), searchControls, (AttributesMapper<Void>) attrs -> {
    results.add(attrs.get("displayName").get().toString());
    return null;
    }, processor);

    System.out.println("size: " + results.size());
    cookie = processor.getCookie();
    hashMore = processor.hasMore();
} while (hashMore);

return results;
© www.soinside.com 2019 - 2024. All rights reserved.