【问题标题】:How to combine two ldap queries / searches - ldap subqueries如何组合两个 ldap 查询/搜索 - ldap 子查询
【发布时间】:2013-05-28 10:26:21
【问题描述】:

我有两个 LDAP JNDI 查询,其中:

1> 获取属于特定组的所有用户的列表

下面是我的代码

String group = StringUtils.isBlank(groupName) ? "*" : groupName
                    .endsWith("*") ? groupName : groupName + "*";
            // Create the search controls
            SearchControls searchCtls = new SearchControls();

            // Specify the search scope
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            // specify the LDAP search filter
            String searchFilter = "(&(objectClass=*)(CN=" + group + "))";

            // Specify the Base for the search
            // String searchBase =
            // "ou=internal,ou=groups,ou=people,dc=somecomp,dc=com";
            String searchBase = "";

            // initialize counter to total the group members
            int totalResults = 0;

            // Specify the attributes to return
            String returnedAtts[] = { "member" };
            searchCtls.setReturningAttributes(returnedAtts);

            // Search for objects using the filter
            NamingEnumeration<?> answer = ctx.search(searchBase, searchFilter,
                    searchCtls);

2> 第二个获取给定用户 ID 的用户的所有属性

这是第二个查询的代码

String attrName = "uid="
                    + userId
                    + ","
                    + (isInternal ? "ou=internal,"
                            : isExternal ? "ou=external,"
                                    : LDAPServicesConstants.EMPTY_STRING)
                    + "ou=people,dc=somecomp,dc=com";
            Attributes attrs = ctx.getAttributes(attrName);
            if (attrs != null) {
                for (NamingEnumeration<?> ae = attrs.getAll(); ae.hasMore();) {
                    Attribute attr = (Attribute) ae.next();
                    String uidAttribute = attr.getID();
                    if (!LDAPHelperUtilities.isSystemAttribute(ctx,
                            uidAttribute)) {
                        ArrayList<String> attrValues = new ArrayList<String>();
                        for (NamingEnumeration<?> attrEnum = attr.getAll(); attrEnum
                                .hasMore(); attrValues.add(String
                                .valueOf(attrEnum.next()))) {
                        }
                        userAttrs.put(uidAttribute.toLowerCase(),
                                (String[]) attrValues
                                        .toArray(new String[0]));
                        log.debug("value(s) : "
                                + Arrays.asList((String[]) userAttrs
                                        .get(uidAttribute.toLowerCase())));
                    }
                }

我需要将这两个查询合并为一个,因为从第一个开始为每个 uid 调用第二个查询不是一种选择(它可能会返回数千个用户)。

有没有一种方法可以将这两者结合起来,并为每个用户返回一个属性集合

谢谢

【问题讨论】:

  • 不回答问题,但肯定objectClass=* 不需要作为过滤器的组件。这是 objectClass 的“存在​​”过滤器,目录中的所有条目都至少有一个 objectClass,因此该过滤器组件不是必需的,并且可能会导致性能问题。

标签: search filter ldap jndi


【解决方案1】:

如果是 Active Directory,我会说使用 (&amp;(objectClass=user)(memberOf=groupDN))

检查您的 LDAP 服务器是否在用户对象上有类似的字段,即指向用户所属组的字段。然后使用该字段构造一个过滤器。因此,您将只有两个查询 - 一个用于组 DN,另一个用于所有用户。

【讨论】:

    【解决方案2】:

    只需将“returnedAtts”从“member”更改为“*”。这为您提供了所有(非操作性)属性。

    【讨论】:

    • 我不确定我是否遵循,这将如何工作。这些查询位于不同的子树上。您能否解释一下,如果我进行更改以删除“成员”“*”,那将如何为属于该特定组的用户提供属性?
    • 如果它们位于不同的子树上,则无法组合它们。 LDAP 没有连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多