2013-06-01 74 views
0

我有两个LDAP JNDI查询,其中:如何合并两个LDAP查询/搜索 - LDAP子查询

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()))); 
        } 
       } 

我需要将这两个查询合并为一个,因为从第一个调用第二个查询是不是一个选项(它可能返回数以千计的用户)。

有没有一种方法可以让我结合这两种,并返回属性的集合的集合,每个用户

感谢您从“成员”

+0

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

回答

1

只是改变“returnedAtts”为“*”。这给你所有(非操作)属性。

+0

我不确定我是否遵循,这将如何工作。这些查询在不同的子树上。你能否解释一下,如果我改变了删除“member”“*”,那么如何给属于那个特定组的用户赋予属性呢? – adbdkb

+0

如果它们在不同的子树上,则不能合并它们。 LDAP没有连接。 – EJP

1

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

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