如何用java动态获取activedirectory的最大页面大小

8nuwlpux  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(432)

当组有超过1500个成员时,我的ldap连接出现问题。我可以用下面的代码片段来解决它。尽管这样做,我还是对硬编码的pagesize变量非常不满意。因为这是在广告服务器上的设置,我不知道这是否和何时会改变。所以我的问题是,如果我能用javax库或任何其他库动态地获得这个pagesize值?如果有人知道一种完全不同的方法来解决这个问题,我也很好奇。我想一定有更好的方法,然后在 generateRangeString(int i) 功能。

package main.java;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.Properties;

public class LdapService {

    private static final int PAGESIZE = 1500;

    public void printAllMembersOfSpecificGroup() throws Exception {
        // Initialize
        LdapContext ldapContext = null;
        NamingEnumeration<SearchResult> results = null;
        NamingEnumeration<?> members = null;

        try {

            Properties properties = new Properties();
            properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            properties.put(Context.PROVIDER_URL, "ldap://url");
            properties.put(Context.SECURITY_AUTHENTICATION, "simple");
            properties.put(Context.SECURITY_PRINCIPAL, "Security Principle");
            properties.put(Context.SECURITY_CREDENTIALS, "password");

            ldapContext = new InitialLdapContext(properties, null);

            int range = 0;
            boolean finish = false;
            while (finish != true) {
                // Set search controls
                SearchControls searchCtls = new SearchControls();
                searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                searchCtls.setReturningAttributes(generateRangeArray(range));

                // Get results
                results = ldapContext.search("base string", String.format("(CN=%s)", "Group name"), searchCtls);
                if (results.hasMoreElements() == true) {
                    SearchResult result = results.next();
                    try {
                        if(result.getAttributes().get(generateRangeString(range)) == null){
                            members = result.getAttributes().get(generateLastRangeString(range)).getAll();
                        } else {
                            members = result.getAttributes().get(generateRangeString(range)).getAll();
                        }
                        while (members.hasMore()) {
                            String distinguishedName = (String) members.next();
                            System.out.println(distinguishedName);
                        }
                        range++;
                    } catch (Exception e) {
                        // Fails means there is no more result
                        e.printStackTrace();
                        finish = true;
                    }
                }
            }
        } catch (NamingException e) {
            throw new Exception(e.getMessage());
        } finally {
            if (ldapContext != null) {
                ldapContext.close();
            }
            if (results != null) {
                results.close();
            }
        }
    }

    public static String[] generateRangeArray(int i) {
        String range = "member;range=" + i * PAGESIZE + "-" + ((i + 1) * PAGESIZE - 1);
        String[] returnedAtts = { range };

        return returnedAtts;
    }

    public static String generateRangeString(int i) {
        String range = "member;range=" + i * PAGESIZE + "-" + ((i + 1) * PAGESIZE - 1);

        return range;
    }
    public static String generateLastRangeString(int i) {
        String range = "member;range=" + i * PAGESIZE + "-" + "*";

        return range;
    }
}
hmtdttj4

hmtdttj41#

如果只使用默认查询策略,请使用“(&(cn=*)”等筛选器在cn=default query policy,cn=query policies,cn=directory service,cn=windows nt,cn=services,cn=configuration,cn=configuration,cn=configuration,域命名上下文(例如cn=default query policy,cn=query policies,cn=directory service,cn=windows nt,cn=services,cn=configuration,dc=example,dc=com)处搜索
返回ldapadminlimits。从属性中解析maxpagesize:
ldapadminlimits(13):maxvalrange=1500;maxreceivebuffer=10485760;maxdatagramrecv=4096;maxpoolthreads=4;maxresultsetsize=262144;最大值=10000;最大持续时间=120;maxpagesize=1000;maxnotificationperconn=5;maxactivequeries=20;maxconnidletime=900;initrecvtimeout=120;最大连接数=5000;
要查找所有查询策略,请在cn=query policies、cn=directory service、cn=windows nt、cn=services、cn=configuration、域命名上下文中搜索(&(objectclass=querypolicy))。。。要么大量研究查询策略,找出如何确定哪个应用于您的连接,要么取最小值并知道您是安全的。

相关问题