简体   繁体   English

Java:LDAP搜索返回1行

[英]Java: LDAP Search returning 1 row

I am trying to get all users from my active directory however my code is returning just one row. 我正在尝试从活动目录中获取所有用户,但是我的代码仅返回一行。 I have tried the below which is currently only outputting one user. 我已经尝试了以下内容,目前仅输出一个用户。

private void getUserBasicAttributes(String username, LdapContext ctx) {

    try {
        List<String> usersList = new ArrayList<String>();
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);


        //First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com"
        //Second Attribute can be uid=username
         NamingEnumeration<SearchResult>  answer = ctx.search("DC=domain,DC=com", "(&(objectCategory=user))"
             , constraints);
        if (answer.hasMoreElements()) {
        Person person = new Person();
           SearchResult  attrs = ((SearchResult) answer.next());
            String names[] = attrs.getName().split(",");
                 String name[] = names[0].split("=");

            usersList.add(name[1]);


        }else{
            throw new Exception("Invalid User");
        }

        System.out.println(usersList.size());

    } catch (Exception ex) {
        ex.printStackTrace();
    }


}

You are not looping over all the results, add a while loop inside the if 您并没有遍历所有结果,请在if内添加一个while循环

if (answer.hasMoreElements()) {
    while(answer.hasMoreElements()) {
        Person person = new Person();
        SearchResult  attrs = ((SearchResult) answer.next());
        String names[] = attrs.getName().split(",");
        String name[] = names[0].split("=");

        usersList.add(name[1]);
    }
}else{
    throw new Exception("Invalid User");
}

You need while instead of if : 您需要while不是if

while (answer.hasMoreElements()) {
    Person person = new Person();
    SearchResult  attrs = ((SearchResult) answer.next());
    String names[] = attrs.getName().split(",");
    String name[] = names[0].split("=");
    usersList.add(name[1]);
}
if (usersList.size() == 0) {
    throw new Exception("Invalid User");
}

You can simplify the name-element handling as well. 您还可以简化名称元素的处理。 No need to parse the DN. 无需解析DN。 Just specify the attribute(s) you want returned up front and retrieve them directly. 只需指定要返回的属性,然后直接检索它们即可。

You are making this too hard. 您太难了。 No reason to perform any "splitting" pf values. 没有理由执行任何“拆分” pf值。

// Specify the ids of the attributes to return
String[] attrIDs = { "uid" };

// Get ONLY the attributes desired
Attributes answer = ctx.getAttributes("CN=Users,DC=YourDomain,DC=com", attrIDs);
for (NamingEnumeration ae = answer.getAll(); ae.hasMore();) {
    Attribute attr = (Attribute)ae.next();
    System.out.println("attribute: " + attr.getID());
    /* Print each value */
    for (NamingEnumeration e = attr.getAll(); e.hasMore();
         System.out.println(e.next()))
        ;
}

Let me know how I can help. 让我知道我将如何提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM