java LDAP 使用简单步骤

项目背景:
某电信项目,使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值