簡體   English   中英

LDAP AD分頁使用java

[英]LDAP AD paging using java

嘗試從 LDAP AD 中的組中獲取成員時,我遇到了一些問題。

1)我需要 1000 的頁面大小,但它返回 1500 個成員(我這邊有什么問題或需要詢問管理員嗎??)

2) Cookie 值始終為 null,我不確定缺少什么。 Cookie 是 null,結果頁面大小是 null

如果您遇到此問題並解決了此問題,請幫助我

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++;
}

您可以使用 Spring PagedResultsDirContextProcessor

您需要確保您的 LdapContextSource 已將 setPooled 設置為true

final PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(1000);

while(processor.hasMore()) {
    ldap.search(base, filter, controls, mapper, processor);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM