Java获取Ldap叶子节点
Java中怎么获取Ldap的某个节点下的素有叶子节点,例如Ldap结构如下:ou=admin|-ou=leaf1|-ou=leaf2|-ou=leaf3说明:ou=adm...
Java中怎么获取Ldap的某个节点下的素有叶子节点,例如Ldap结构如下:
ou=admin
|-ou=leaf1
|-ou=leaf2
|-ou=leaf3
说明:ou=admin是一个节点,它下面有三个叶子节点leaf1、leaf2、leaf3
只提供ou=admin,怎么获取三个叶子节点?
答案准确必有加分! 展开
ou=admin
|-ou=leaf1
|-ou=leaf2
|-ou=leaf3
说明:ou=admin是一个节点,它下面有三个叶子节点leaf1、leaf2、leaf3
只提供ou=admin,怎么获取三个叶子节点?
答案准确必有加分! 展开
1个回答
展开全部
使用netscape的ldap工具包。使用其它的工具包,写法类似。
注:必须知道base dn,以下假定ou=admin为base dn。若有明确的base dn,那写法比如:ou=admin,o=test1
源代码如下:
==========================
import netscape.ldap.*;
public class SearchTest {
public static void main(String[] args) {
LDAPConnection lc = null;
LDAPEntry findEntry = null;
int status = -1;
try {
// lc = ConnectionPool.
lc = new LDAPConnection();
/* Connect to server */
String MY_HOST = "localhost";
int MY_PORT = 389;
lc.connect(MY_HOST, MY_PORT);
System.out.println("1=========" +lc.isConnected());
lc.authenticate("cn=Directory Manager","password");
System.out.println("2=========" +lc.isConnected());
/* search for all entries with surname of Jensen */
String MY_FILTER = "(objectclass=*)";
String MY_SEARCHBASE = "ou=admin";
LDAPSearchConstraints cons = lc.getSearchConstraints();
cons.setBatchSize(0);
cons.setMaxResults(5);
MY_FILTER = "(objectclass=*)" ;
LDAPSearchResults res = lc.search(MY_SEARCHBASE,
LDAPConnection.SCOPE_ONE,
MY_FILTER,
null,
false,
cons);
while (res.hasMoreElements()) {
LDAPEntry cLDAPEntry = res.next() ;
System.out.println("" + cLDAPEntry.getDN());
System.out.println("" + cLDAPEntry.toString());
}
status = 0;
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
e.printStackTrace();
}
/* Done, so disconnect */
if ((lc != null) && lc.isConnected()) {
try {
lc.disconnect();
System.out.println("3=========" +lc.isConnected());
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
}
System.out.println("4=========" +lc.isConnected());
System.exit(status);
}
}
注:必须知道base dn,以下假定ou=admin为base dn。若有明确的base dn,那写法比如:ou=admin,o=test1
源代码如下:
==========================
import netscape.ldap.*;
public class SearchTest {
public static void main(String[] args) {
LDAPConnection lc = null;
LDAPEntry findEntry = null;
int status = -1;
try {
// lc = ConnectionPool.
lc = new LDAPConnection();
/* Connect to server */
String MY_HOST = "localhost";
int MY_PORT = 389;
lc.connect(MY_HOST, MY_PORT);
System.out.println("1=========" +lc.isConnected());
lc.authenticate("cn=Directory Manager","password");
System.out.println("2=========" +lc.isConnected());
/* search for all entries with surname of Jensen */
String MY_FILTER = "(objectclass=*)";
String MY_SEARCHBASE = "ou=admin";
LDAPSearchConstraints cons = lc.getSearchConstraints();
cons.setBatchSize(0);
cons.setMaxResults(5);
MY_FILTER = "(objectclass=*)" ;
LDAPSearchResults res = lc.search(MY_SEARCHBASE,
LDAPConnection.SCOPE_ONE,
MY_FILTER,
null,
false,
cons);
while (res.hasMoreElements()) {
LDAPEntry cLDAPEntry = res.next() ;
System.out.println("" + cLDAPEntry.getDN());
System.out.println("" + cLDAPEntry.toString());
}
status = 0;
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
e.printStackTrace();
}
/* Done, so disconnect */
if ((lc != null) && lc.isConnected()) {
try {
lc.disconnect();
System.out.println("3=========" +lc.isConnected());
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
}
System.out.println("4=========" +lc.isConnected());
System.exit(status);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询