如何使用Java从活动目录中的特定ou获取所有用户?

时间:2018-12-24 04:36:09

标签: java ldap ou

这是我曾经用来获取但没有填充任何东西的方法。

public void doSearch() throws NamingException {
        String searchFilter = "(&(ou=Example,ou=Examples_ou)(objectClass=person))";
        String domain = "DC=mydom,DC=com";
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration<SearchResult> answer = ctx.search(domain, searchFilter, searchControls);
        int ttl = 0;
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult) answer.next();
            ttl++;
            System.out.println(">>>" + sr.getName());
            Attributes attrs = sr.getAttributes();
            System.out.println(">>>>>>" + attrs.get("samAccountName"));
        }
        System.out.println("Total results: " + ttl);
    }

1 个答案:

答案 0 :(得分:1)

您的过滤器无效,因此不返回任何数据。在

ctx.search(domain, searchFilter, searchControls);

被作为搜索的基本DN传递。如果要将搜索限制在ou = Example,ou = dc = example,dc = com的ou = Examples_ou之内,则搜索基准DN应该为“ ou = Example,ou = Examples_ou,dc = example,dc = com”和searchFilter只是“(&(objectClass = person))”