gpt4 book ai didi

java - 使用java进行LDAP AD分页

转载 作者:行者123 更新时间:2023-11-30 04:16:28 32 4
gpt4 key购买 nike

我在尝试从 LDAP AD 中的组中获取成员时遇到了一些问题。

1) 我需要 1000 的页面大小,但它返回 1500 个成员(我这边有什么问题吗?或者需要询问管理员?)

2) Cookie 值始终为 null,我不确定缺少什么。 Cookie 为空,resultpagesize 为空

如果您遇到此问题并解决了此问题,请帮助我

Hashtable<String, Object> env = new Hashtable<String, Object>();
LdapContext ctx;
byte[] cookie = null;
try {
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost");
env.put(Context.SECURITY_PRINCIPAL,"cn=testaccount");
env.put(Context.SECURITY_CREDENTIALS, "passwd");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
ctx = new InitialLdapContext(env, null);
SearchControls searchCtls = new SearchControls();
String returnedAtts[]={"member"};
searchCtls.setSearchScope(2);
searchCtls.setReturningAttributes(returnedAtts);
ctx.setRequestControls(new Control[] { new PagedResultsControl(1000, false) });
do {
NamingEnumeration answer = ctx.search("", "(&(objectClass=group)(cn=testgroup))", searchCtls);
while (answer.hasMore()) {
SearchResult entry = (SearchResult) answer.next();
String attrsValaues = entry.getAttributes().toString();
System.out.println(attrsValaues);
}
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
System.out.println(controls[i]);
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
cookie = prrc.getCookie();
}
}
}
ctx.setRequestControls(new Control[] { new PagedResultsControl(1000, cookie, false) });
} while(cookie!=null);
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

我找到了一种无需使用 cookie 即可获取用户的方法

boolean endString = true;
int loopValue = 0;
while (endString) {
int startValue = loopValue * 1000;
int endvalue = (loopValue + 1) * 1000;
SearchControls searchCtls = new SearchControls();
String[] returnedAttrs = new String[1];
String range = startValue + "-" + endvalue;
returnedAttrs[0] = "member;range=" + range;
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setReturningAttributes(returnedAttrs);
NamingEnumeration answer = ctx.search("", "(&(objectClass=group)(cn=testgroup))", searchCtls);
while (answer.hasMore()) {
SearchResult entry = (SearchResult) answer.next();
System.out.println(entry.getAttributes());
if (entry.getAttributes().toString().contains("{member;range=" + startValue + "-*")) {
endString = false;
}
}
loopValue++;
}

关于java - 使用java进行LDAP AD分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221630/

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