Java 在ldap中通过cn在ou=users、ou=system目录中搜索用户

Java 在ldap中通过cn在ou=users、ou=system目录中搜索用户,java,ldap,Java,Ldap,我试图在用户目录(ou=users,ou=system)中搜索用户,但没有得到结果,请帮助我。 以下是我搜索用户目录的代码 public void search(String uid) { String searchBase = "ou=users,ou=system"; env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.pu

我试图在用户目录(ou=users,ou=system)中搜索用户,但没有得到结果,请帮助我。 以下是我搜索用户目录的代码

    public void search(String uid) {
    String searchBase = "ou=users,ou=system";

    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_CREDENTIALS, rootpass);
    DirContext ctx = null;
    try {

![enter image description here][1]      // Create the initial directory context
        ctx = new InitialDirContext(env);

        // Create the search controls
        SearchControls searchCtls = new SearchControls();

        // Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchCtls.setReturningAttributes(new String[] { "uid", "cn" });

        String searchFilter =" (uid="+uid+") ";//"(objectclass=*)"; //" (uid="+uid+") ";

        // initialize counter to total the results
        int totalResults = 0;

        // Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                searchCtls);

        while (answer.hasMore()) {
            SearchResult sr = (SearchResult) answer.next();

            totalResults++;

            System.out.println(">>>" + sr.getName());
            System.out.println(">>>");
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
}
这是我的目录结构

您可能想看看。

如果“没有打印任何内容,但当我在任何其他目录中搜索时,它工作正常”,则可能归结为:

  • 您根本无法连接到LDAP目录

  • 你的搜索库错了

  • 你的过滤器错了

  • 您正在搜索的记录不存在

  • 您用于绑定的凭据没有在该位置搜索的权限


其中一些将抛出NamingException,但其他(如“记录不存在”或“没有搜索权限”)将不返回任何结果。

对于用户文件夹,答案在另一篇文章中

这可能看起来很愚蠢,但Active Directory中的默认树设置不是OU=Users,dc=domain,dc=com,而是CN=Users,dc=domain,dc=com(注意CN=not OU=for Users)


它在哪里?结构是…您是否确保已成功登录(是否匿名登录?)是否收到任何错误?如果没有,打印的内容是什么?还可以尝试调用getAttributes()在SearchResults上,查看所有属性。没有打印任何内容。但是当我在任何其他目录中搜索时,它工作正常