Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search 如何组合两个ldap查询/搜索-ldap子查询_Search_Filter_Ldap_Jndi - Fatal编程技术网

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

Search 如何组合两个ldap查询/搜索-ldap子查询,search,filter,ldap,jndi,Search,Filter,Ldap,Jndi,我有两个LDAP JNDI查询,其中: 1> 一个获取属于特定组的所有用户的列表 下面是我的代码 String group = StringUtils.isBlank(groupName) ? "*" : groupName .endsWith("*") ? groupName : groupName + "*"; // Create the search controls SearchControls se

我有两个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);
String group=StringUtils.isBlank(groupName)?“*”:组名
.endsWith(“*”)?groupName:groupName+“*”;
//创建搜索控件
SearchControls searchCtls=新的SearchControls();
//指定搜索范围
searchCtls.setSearchScope(SearchControls.SUBTREE_范围);
//指定LDAP搜索筛选器
字符串searchFilter=“(&(objectClass=*)(CN=“+group+”)”;
//指定搜索的基数
//字符串搜索库=
//“ou=内部,ou=组,ou=人员,dc=somecomp,dc=com”;
字符串searchBase=“”;
//初始化计数器以合计组成员
int totalResults=0;
//指定要返回的属性
字符串returnedAtts[]={“member”};
searchCtls.设置ReturningAttributes(returnedAtts);
//使用过滤器搜索对象
NamingEnumeration answer=ctx.search(searchBase、searchFilter、,
搜索(CTL);
2> 第二个获取给定userid的用户的所有属性

这是第二个查询的代码

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())));
                    }
                }
String attrName=“uid=”
+用户ID
+ ","
+(isInternal?“ou=内部,”
:isExternal?“ou=外部,”
:LDAPServicesConstants.空(字符串)
+“ou=people,dc=somecomp,dc=com”;
Attributes attrs=ctx.getAttributes(attrName);
如果(属性!=null){
for(namingumeration ae=attrs.getAll();ae.hasMore();){
属性attr=(属性)ae.next();
字符串uidAttribute=attr.getID();
如果(!ldaphelpureutilities.isSystemAttribute(ctx,
uidAttribute){
ArrayList attrValues=新的ArrayList();
对于(NamingEnumeration attrEnum=attr.getAll();attrEnum
.hasMore();attrValues.add(字符串
.valueOf(attrEnum.next())){
}
userAttrs.put(uidAttribute.toLowerCase(),
(字符串[])属性值
.toArray(新字符串[0]);
log.debug(“值:”
+Arrays.asList((字符串[])userAttrs
.get(uidAttribute.toLowerCase());
}
}
我需要将这两个查询合并为一个查询,因为从第一个查询为每个uid调用第二个查询不是一个选项(它可能返回数千个用户)

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


谢谢

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

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


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

我不确定我是否遵循,这将如何工作。这些查询在不同的组上ubtrees。如果我更改为删除“成员”*,您能解释一下吗,这将如何为属于该特定组的用户提供属性?如果他们位于不同的子树上,则无法将其组合。LDAP没有联接。不回答这个问题,但肯定
objectClass=*
不是筛选器的必需组件。这是objectClass的“当前”筛选器,以及di中的所有条目目录至少有一个objectClass,因此该筛选器组件不是必需的,可能会导致性能问题。