如何通过Java中的LDAP获取AD组的所有成员

问题描述 投票:1回答:2

我编写了一个应用程序来检索Active Directory组并展平它们,即包括子组的递归成员到顶级父组。它适用于小团体,但对于较大的团体,我遇到了问题。

如果成员数不超过1500,则会在成员属性中列出。如果还有更多 - 则此属性为空且属性名为member;范围:0-1499,包含前1500个成员。

我的问题是我不知道如何让其他成员超过1500.我们有8-12万名成员的团体。我需要运行另一个查询吗?在微软网站上,我看到了类似问题的C#代码片段,但对它没有多大意义,因为它们展示了如何指定范围,而不是如何将其插入查询。如果有人知道如何用Java做,我会很感激。

java active-directory ldap
2个回答
1
投票

这显然会给你下一个:

String[] returnedAtts = { "member;range=1500-2999" };

您需要按块(1500块)获取用户块只需创建一个计数器并更新搜索并检索下一个,直到您拥有所有这些块。


1
投票

在您的帮助下,我有一个完整的工作代码

    // Initialize
    LdapContext ldapContext = null;
    NamingEnumeration<SearchResult> results = null;
    NamingEnumeration<?> members = null;

    try {
        // Initialize properties
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        properties.put(Context.PROVIDER_URL, "ldap://" + ldapUrl);
        properties.put(Context.SECURITY_PRINCIPAL, adminLoginADOnPremise);
        properties.put(Context.SECURITY_CREDENTIALS, adminPasswordADOnPremise);

        // Initialize ldap context
        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(ldapBaseDn, String.format("(samAccountName=%s)", groupName), searchCtls);
            if (results.hasMoreElements() == true) {
                SearchResult result = results.next();
                try {
                    members = result.getAttributes().get(generateRangeString(range)).getAll();
                    while (members.hasMore()) {
                        String distinguishedName = (String) members.next();
                        logger.debug(distinguishedName);
                    }
                    range++;
                } catch (Exception e) {
                    // Fails means there is no more result
                    finish = true;
                }
            }
        }
    } catch (NamingException e) {
        logger.error(e.getMessage());
        throw new Exception(e.getMessage());
    } finally {
        if (ldapContext != null) {
            ldapContext.close();
        }
        if (results != null) {
            results.close();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.