如何组合两个ldap查询/搜索 - ldap子查询

时间:2013-06-01 20:26:23

标签: search filter ldap jndi

我有两个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 - ;第二个获取给定用户标识的用户的所有属性

这是第二个查询的代码

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的第二个不是一个选项(它可能会返回数千个用户)。

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

谢谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

相关问题