我试图在用户目录中搜索用户(ou=users,ou=system),但我没有得到结果PLZ帮助我。以下是搜索用户目录
的代码 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,但其他的(如"记录不存在"或"没有权限搜索")将简单地不返回任何结果。
就User文件夹而言,答案在另一篇文章中。net中的LDAP目录条目-不与OU=Users一起工作
这可能看起来很愚蠢,但是Active Directory中的默认树设置是而不是OU=Users,dc=domain,dc=com,而是CN=Users,dc=domain,dc=com(注意CN=而不是OU= for Users)。