在多个线程中使用JNDI引入Active Directory用户

时间:2017-01-19 08:45:43

标签: multithreading powershell active-directory jndi

我设计了一个应用程序,它将用户从活动目录带到MySQL数据库,并在GUI上显示它们。它还带来了用户所属的组。

所以,我的程序以这种方式工作:

for(String domain : allConfiguredADomains)  {
    LdapContext domainCtx = getDomainCtx(domain);

    // Bring all users from this domain and store them in DB
    getAllUsersForDomain(domain, domainCtx);

    // Bring all the groups for every user
    getAllGroupsForUsersInTheDomain(domain, domainCtx)
}

void getAllUsersForDomain(String domain, LdapContext domainCtx) {
    String filter = "(objectClass=User)"

    NamingEnumeration<SearchResult> result = domainCtx.search(domain, filter, ..);
    while(result.hasMoreElements()) {
        SearchResult searchResult = (SearchResult) result.nextElement();

        // Process and store in database
        storeUserInDatabase(searchResult);
    }
}

void getAllGroupsForUsersInTheDomain(String domain, LdapContext domainCtx)  {

    List<String> userDistinguishedNames = getAllUsersFromDatabase("distinguishedName");

    for(String userDn : userDistinguishedNames) {
        String filter = "(&(objectClass=Group)(distinguishedName=" + userDn + "))";

        NamingEnumeration<SearchResult> result = domainCtx.search(domain, filter, ..);

        List<String> allGroupsOfUser = new List<String>();
        while(result.hasMoreElements()) {
            SearchResult searchResult = (SearchResult) result.nextElement();

            String groupDistinguishedName = searchResult.getAttributes().get("distinguishedName").get();
            allGroupsOfUser.add(groupDistinguishedName);
        }

        // Store them in database
        storeAllGroupsOfUserInDatabase(userDn, allGroupsOfUser);
    }
}

但是,当活动目录中的用户太多时,此应用程序会花费大量时间。所以,我决定实现并行(使用线程)。我使用用户distinguishedName上的搜索过滤器对此进行了划分。

String filter = "(&(objectClass=User)(distinguishedName=a*"))";

等等..在每个线程中获取用户。

  • 我的表现更好,但仍然不太好。有人可以建议 更好的方法?
  • 另外,我不知道如何介绍 取组时的并行性?
  • 如果有人建议使用PowerShell或C#做得更好,请建议我对技术持开放态度。

请注意:阅读用户属性memberOf并未提供所有群组,因此我会单独提取群组。

1 个答案:

答案 0 :(得分:0)

我不是Active Directory专家 - 只是想分享一些想法。

  1. 按字母表线程进行线程处理最多允许26个线程。您是否考虑过通过其他一些属性,组成员身份等创建搜索线程?这可能会让您创建更多线程。

  2. 查看Active Directory文档以查看是否有提高搜索性能的方法(例如,使用数据库我们可以创建索引)。

相关问题