gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:31:21 25 4
gpt4 key购买 nike

我正在尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com