如何在Java中设置LDAP客户端请求的大小限制?

时间:2013-06-26 11:46:15

标签: java ldap jndi

An article about best practices in LDAP searches州:

  

程序员应始终提供客户端请求的大小限制。

如何在Java中完成?我在documentation中找不到任何合适的选项。

有人可以给我一个提示吗?

2 个答案:

答案 0 :(得分:2)

如果客户端使用的是UnboundID LDAP SDK:

SearchRequest req = new SearchRequest(baseObject,scope,filter,requestedAttributes);
req.setSizeLimit(maxNumberOfEntriesToReturn);

如果客户端正在使用JNDI,请使用setCountLimit,并考虑使用UnboundID LDAP SDK而不是JNDI来获取新代码。

答案 1 :(得分:0)

.setSizeLimit()无助于加强服务器大小限制。

并且此代码也

                SearchControls ctls = new SearchControls();
                ctls.setCountLimit(99000);

您应该使用分页模式。

public static void main(String[] args) {

try {
    int count = 0;
    LDAPConnection connection = new LDAPConnection("hostname", 389, "user@domain", "password");

    final String path = "OU=Users,DC=org,DC=com";
    String[] attributes = {"SamAccountName","name"};

    SearchRequest searchRequest = new SearchRequest(path, SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person"), attributes);

    ASN1OctetString resumeCookie = null;
    while (true)
    {
        searchRequest.setControls(
                new SimplePagedResultsControl(100, resumeCookie));
        SearchResult searchResult = connection.search(searchRequest);
        for (SearchResultEntry e : searchResult.getSearchEntries())
        {
            if (e.hasAttribute("SamAccountName"))
                System.out.print(count++ + ": " + e.getAttributeValue("SamAccountName"));

            if (e.hasAttribute("name"))
                System.out.println("->" + e.getAttributeValue("name"));
        }

        LDAPTestUtils.assertHasControl(searchResult,
                SimplePagedResultsControl.PAGED_RESULTS_OID);
        SimplePagedResultsControl responseControl =
                SimplePagedResultsControl.get(searchResult);
        if (responseControl.moreResultsToReturn())
        {
            resumeCookie = responseControl.getCookie();
        }
        else
        {
            break;
        }
    }


}
catch (Exception e)
{
    System.out.println(e.toString());
}

}