我遇到了搜索LDAP的问题。如果我使用以下代码,我可以使用以下代码获得级别2。 但我想获得Level 4对象。谢谢你的帮助。
当前搜索群:ou=HQ2-BR
,过滤器:"(ou=*)";
此致 曼帕洪,戴夫 manpakhong@hotmail.com manpakhong@gmail.com
LDAP结构
代码:
public void loopLDAP() {
String adminName = "uid=writer,ou=People,o=com,dc=rabbitforever";
String adminPassword = "password";
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
//env.put(Context.PROVIDER_URL,
// "ldap://192.168.1.127:389/dc=rabbitforever,dc=com");
env.put(Context.PROVIDER_URL,
"ldap://10.10.176.156:389/o=com,dc=rabbitforever");
//env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.REFERRAL, "follow");
try {
LdapContext ctx = new InitialLdapContext(env, null);
ctx.setRequestControls(null);
String filter = "(ou=*)";
NamingEnumeration<?> namingEnum = ctx.search("ou=HQ2-BR", filter,
getSimpleSearchControls());
while (namingEnum.hasMore()) {
SearchResult result = (SearchResult) namingEnum.next();
Attributes attrs = result.getAttributes();
String cn = "";
String sn = "";
String description = "";
String uid = "";
if (null != attrs.get(cn)) {
cn = attrs.get("cn").toString();
}
if (null != attrs.get("sn")) {
sn = attrs.get("sn").toString();
}
if (null != attrs.get("description")) {
description = attrs.get("description").toString();
}
if (null != attrs.get("uid")) {
uid = attrs.get("uid").toString();
}
System.out.println(cn + " | " + sn + " | " + description
+ " | " + uid);
}
} catch (Exception ex) {
ex.printStackTrace();
}
} // end loopLDAP()
答案 0 :(得分:2)
您可能需要使用SearchControls
构建SearchControls.SUBTREE_SCOPE
对象,并将其传递给ctx.search
方法。请参阅另一个答案中的example。