我正在尝试在用户目录(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();
}
}
这是我的目录结构