项目背景:
某电信项目,使用LDAP存储用户信息。现在需要根据用户的“电话号码”获取其全部信息。
关键代码(我当时不会的):
详细代码:
可供参考的网站:
"Download URL:"
http://www.findjar.com/jar/com.novell.ldap/jars/jldap-4.3.jar.html
"Tutorial"
http://quark.humbug.org.au/publications/ldap/ldap_tut.html
某电信项目,使用LDAP存储用户信息。现在需要根据用户的“电话号码”获取其全部信息。
关键代码(我当时不会的):
[size=large][color=green]
// LDAPEntry nextEntry = searchResults.next();
// LDAPAttributeSet attSet = nextEntry.getAttributeSet();
// for (Iterator iterator = attSet.iterator(); iterator.hasNext();) {
// LDAPAttribute att = (LDAPAttribute) iterator.next();
// LDAPattrNameValue.add(att.getName() + ": " + att.getStringValue());
//}
[/color][/size]
详细代码:
// ConnectDIR
String ldapHost = "xxx.xxx.xxx.xxx";
// To get user's id
String loginDN = "0001";
// To get user's password
String password = "admin"
LDAPConnection lc = new LDAPConnection();
// connect the "LDAP Server"
lc.connect(ldapHost, LDAPConnection.DEFAULT_PORT);
// Binding "LDAP Server"
lc.bind(LDAPConnection.LDAP_V3, loginDN, password.getBytes("UTF8"));
//SearchBase、SearchFilter、SearchScope
String strSearchBase = CLdapBasednInfo.getBaseDnNe();
String strsearchFilter = "cn=" + telephoneNO.replaceFirst("0{1}", "");
int intSearchScope = LDAPConnection.SCOPE_SUB;
// search the LDAP BY the telephoneNO TO get ENTRY.
LDAPSearchResults searchResults = lc.search(strSearchBase,intSearchScope,strsearchFilter,null,false);
// searchResults.hasMore()
while (searchResults.hasMore()) {
LDAPEntry nextEntry = null;
try {
nextEntry = searchResults.next();
} catch (LDAPException ldape) {
throw ldape;
}
dn = nextEntry.getDN();
LDAPattrNameValue.add("dn:" + dn);
LDAPAttributeSet attSet = nextEntry.getAttributeSet();
for (Iterator iterator = attSet.iterator(); iterator.hasNext();) {
LDAPAttribute att = (LDAPAttribute) iterator.next();
LDAPattrNameValue.add(att.getName() + ": " + att.getStringValue());
}
}
lc.disconnect();
// catch(LDAPException ldape)
// ===
// LDAPEntry nextEntry = searchResults.next();
// LDAPAttributeSet attSet = nextEntry.getAttributeSet();
// for (Iterator iterator = attSet.iterator(); iterator.hasNext();) {
// LDAPAttribute att = (LDAPAttribute) iterator.next();
// LDAPattrNameValue.add(att.getName() + ": " + att.getStringValue());
//}
可供参考的网站:
"Download URL:"
http://www.findjar.com/jar/com.novell.ldap/jars/jldap-4.3.jar.html
"Tutorial"
http://quark.humbug.org.au/publications/ldap/ldap_tut.html