java - 为什么 Active Directory 不返回 PagedResultsResponseControl?

标签 java active-directory ldap jndi

我正在尝试从 Java 枚举我们 Active Directory 中的所有组。有很多,所以我在 1000 个结果后得到一个 SizeLimitExceededException。我正在尝试使用 PagedResultsControl,我的代码非常接近网络上所有示例的模型,并且它可以正常工作,因为它不再抛出 SizeLimitExceededException,并返回匹配指定页面大小的结果数(前提是不大于 1000)。

但是,下一步是从响应中获取 cookie 并使用它来获取下一页,我的问题是在调用 搜索()。事实上 getResponseControls() 返回 null

我进行了广泛的搜索,似乎找不到任何其他人报告此问题,我几乎被困在这里。那我做错了什么?为什么我没有得到 PagedResultsResponseControl

我们的域在 Windows Server 2016 上运行,我已将我的代码缩减为以下测试用例:

public class PagingTest {
    public static void main(String[] args) throws Exception {
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_PRINCIPAL, "username");
        env.put(Context.SECURITY_CREDENTIALS, "password");
        env.put(Context.PROVIDER_URL, "ldap://campus.uni.ac.uk/DC=campus,DC=uni,DC=ac,DC=uk");
        LdapContext adContext = new InitialLdapContext(env, null);

        // Set up search controls.
        SearchControls searchControl = new SearchControls();
        searchControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String[] attributesToFetch = {"cn"};
        searchControl.setReturningAttributes(attributesToFetch);

        // Set up a paged search.
        final int pageSize = 500;
        byte[] cookie = null;
        adContext.setRequestControls(new Control[]{
                new PagedResultsControl(pageSize, Control.CRITICAL)
                });

        // Do the search.
        int count = 0;
        boolean finished = false;
        while (!finished) {
            NamingEnumeration<SearchResult> records
                    = adContext.search("OU=Groups", "objectClass=group", searchControl);

            // Examine the page's results control response and act accordingly.
            Control[] controls = adContext.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();
                        if (cookie == null) {
                            finished = true;
                        }
                    }
                }
            } else {
                cookie = null;
                finished = true;
            }

            // Process the page of results.
            while (records != null && records.hasMore()) {
                SearchResult sr = records.next();
                Attributes attribs = sr.getAttributes();
                BasicAttribute ba = (BasicAttribute) attribs.get("cn");
                String cn = (String) ba.get();
                System.out.println(cn);
                ++count;
            }

            // Re-activate paged results with the new cookie.
            adContext.setRequestControls(new Control[]{
                    new PagedResultsControl(pageSize, cookie, Control.CRITICAL)
                    });
        }
        System.out.println("Found " + count + " groups");
    }
}

最佳答案

可能你的ldap服务器不支持分页查询,你可以像这样使用ldapsearch命令:

ldapsearch -H ldap://xxxx:389 -x -D "uid=zhangsan,ou=employee,dc=test,dc=com" -W -b "" -s base -a always "(objectClass=*)" "supportedControl"

如果返回值中包含1.2.840.113556.1.4.319,说明您的ldap服务器支持分页查询

关于java - 为什么 Active Directory 不返回 PagedResultsResponseControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51878159/

相关文章:

database - 启用与 LDAP 服务器的 SSL 通信

java - 在不使用 Scanner 的情况下从控制台获取用户输入

java - 从 List/ArrayList 获取最后三个元素?

Azure ACR 存储库访问

windows-7 - "Active Directory Users and Computers"Windows 7 的 MMC 管理单元?

c# - 将密码存储在加密的 cookie 中?

java - 如何将 Openllet OWL2 推理器(或任何其他)与 Jena TDB 一起使用?

java - 读取 Excel 文件的内存高效 Java 库?

java - 在 LDAP 中存储照片

linux - 尝试从 Linux 命令行运行 LDAP 查询