使用java的LDAP AD分页

时间:2013-08-14 01:04:52

标签: java active-directory ldap spring-ldap

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

1)我需要页面大小为1000,但它返回1500个成员(myside是否有任何问题或者需要请求管理员?)

2)Cookie值始终为null,我不确定缺少什么。 Cookie为null,resultpagesize为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();
}

1 个答案:

答案 0 :(得分:2)

我找到了一种不使用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++;
}