用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(一)
關(guān)鍵字: 用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(一)
LDAP現(xiàn)在用的越來(lái)越多,所謂LDAP既Lightweight Directory Access Protocol。關(guān)于它的一些基本知識(shí),我在這里就不做系統(tǒng)的介紹了,網(wǎng)上有很多的資料。我主要說(shuō)一下在JAVA的語(yǔ)言環(huán)境中,怎樣來(lái)操作LDAP。
在這里,我推薦兩個(gè)工具:LDAPTemplate和JLDAP。
網(wǎng)上的資料比較少,而且不少都是E文的,可能英語(yǔ)不太好的朋友,就很難入門(mén)了。在這我把我的經(jīng)驗(yàn)總結(jié)一下,和大家分享。
LDAPTemplate是基于Spring1.2.7來(lái)開(kāi)發(fā)的,其用法和Spring的JDBCTemplate差不多。最初,我是用這個(gè)開(kāi)源的框架來(lái)對(duì)LDAP進(jìn)行操作的,但是后來(lái)由于開(kāi)發(fā)工具的轉(zhuǎn)變,由eclipse轉(zhuǎn)到了RAD上,而RAD所用的JDK卻不支持Spring1.2.7(看來(lái)網(wǎng)上的謠傳沒(méi)錯(cuò),IBM總在某個(gè)陰暗的角落在和SUN作對(duì)),沒(méi)辦法,只好另辟蹊徑。后來(lái)發(fā)現(xiàn)了JLDAP,經(jīng)過(guò)一番研究,發(fā)現(xiàn)它用起來(lái)并不比LDAPTemplate復(fù)雜,但在對(duì)象持久化方面需要自己去做,而LDAPTemplate通過(guò)AttributeMappers就可以把查詢(xún)到的結(jié)果轉(zhuǎn)換成POJO了。
用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(二)
關(guān)鍵字: 用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(二)
下面來(lái)具體的說(shuō)一下怎么用JLDAP。首先要去下載一下JLDAP,具體下載的地址可以上網(wǎng)去搜。下載下來(lái)以后,lib里面的是開(kāi)發(fā)所要用到的包,doc里面是幫助文檔API和示例程序。
先說(shuō)說(shuō)怎么查詢(xún),其實(shí)查詢(xún)非常的簡(jiǎn)單,如果用過(guò)JDBC連數(shù)據(jù)庫(kù)的話,那么連LDAP相比起來(lái)更加的簡(jiǎn)單。
首先建立一個(gè)LDAPConnection對(duì)象。這個(gè)對(duì)象也可以通過(guò)連接池PoolManager來(lái)獲得。LDAPConnection con = new LDAPConnection();然后運(yùn)行connect方法和bind方法。連接上LDAP以后,就可以通過(guò)search方法來(lái)查找數(shù)據(jù)了。示例程序如下:
java 代碼
LDAPConnection lc = new LDAPConnection();
try {
lc.connect("6.1.19.154",389);
lc.bind(LDAPConnection.LDAP_V3,"cn=xxx","xxxxxx");
LDAPSearchResults rs = lc.search("dc=excel,dc=com,dc=cn",LDAPConnection.SCOPE_SUB,"objectClass=*",null,false);
int count = 0;
while(rs.hasMore()){
LDAPEntry entry = rs.next();
System.out.println(entry.getDN());
count++;
}
System.out.println("共有"+count+"條記錄。");
} catch (LDAPException e) {
System.err.print("連接異常! ");
e.printStackTrace();
}
10:33 | 永久鏈接 | 瀏覽 (1717) | 評(píng)論 (5) | 收藏 | ldap | 進(jìn)入論壇 |
永久鏈接
http://junewolf.javaeye.com/blog/52090
評(píng)論 共 5 條 發(fā)表評(píng)論
Dustbin 2007-03-30 16:39
這樣的程序會(huì)導(dǎo)致ldap服務(wù)器死機(jī)地,需要關(guān)閉ldap連接
Hejrcc 2007-06-14 21:05
呵呵, 就是。。。
Hejrcc 2007-06-14 21:10
看不出用JLDAP有什么優(yōu)勢(shì), 我也剛剛開(kāi)始學(xué)。
我寫(xiě)了個(gè)測(cè)試?yán)樱?qǐng)指點(diǎn):
代碼
public void testLdap() {
try {
DirContext context = getContext();
addEntry(context, "uid=oracle,ou=people,dc=mycompany,dc=com");
printEntry(context, "uid=oracle,ou=people,dc=mycompany,dc=com");
context.close();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}
public DirContext getContext() throws NamingException {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=mycompany,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "secret");
env.put(Context.SECURITY_AUTHENTICATION, "simple"); //"none", "simple", "strong"
DirContext initial = new InitialDirContext(env);
DirContext context = (DirContext) initial.lookup("ldap://localhost:389");
return context;
}
public void addEntry(DirContext context, String dn) throws NamingException {
Attributes attrs = new BasicAttributes();
attrs.put("uid", "oracle");
attrs.put("sn", "Lee");
attrs.put("cn", "Amy Lee");
attrs.put("telephoneNumber", "+1 408 555 0033");
attrs.put("userPassword", "redqueen".getBytes());
//the following attribute has two values
Attribute objclass = new BasicAttribute("objectClass");
objclass.add("uidObject");
objclass.add("person");
attrs.put(objclass);
context.createSubcontext(dn, attrs);
}
Hejrcc 2007-06-14 21:13
看見(jiàn)還有一種寫(xiě)法用來(lái)獲取 DirContext, 下面的寫(xiě)法指定了 INITIAL_CONTEXT_FACTORY屬性,我想知道我前面一種寫(xiě)法里面, env.put(Context.INITIAL_CONTEXT_FACTORY, ?);
這個(gè)INITIAL_CONTEXT_FACTORY 我沒(méi)有設(shè)置, 不知道默認(rèn)是什么?
代碼
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=mycompany,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "secret");
//env.put(Context.SECURITY_AUTHENTICATION, "simple"); //"none", "simple", "strong"
DirContext context = new InitialDirContext(env);
用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(三)
關(guān)鍵字: 用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(三)
雖然LDAP主要是用來(lái)進(jìn)行讀操作的,但不可避免的,我們也要向其中添加一些新的數(shù)據(jù)。用JLDAP向LDAP服務(wù)器添加數(shù)據(jù)的操作也是非常簡(jiǎn)單的。
為什么說(shuō)非常簡(jiǎn)單呢,因?yàn)榇篌w上也就是分三步。第一步,連接LDAP服務(wù)器。第二步,建立一個(gè)要添加的新的實(shí)體LDAPEntry,并添加相應(yīng)的屬性。第三步,通過(guò)add方法向LDAP中添加實(shí)體。
首先說(shuō)連接服務(wù)器。還是非常簡(jiǎn)單的三步:
java 代碼
LDAPConnection con = new LDAPConnection();
con.connect("hostname",hostport);
con.bind("version","DN","password");
連接后,可以建實(shí)體了,也就相當(dāng)與為數(shù)據(jù)庫(kù)添加一條新的記錄。這里用到了幾個(gè)類(lèi):LDAPEntry、LDAPAttribute和LDAPAttributeSet。首先建立一個(gè)LDAPAttributeSet,然后建立各種的LDAPAttribute,把他們add到LDAPAttributeSet中。然后建立一個(gè)LDAPEntry。其構(gòu)造函數(shù)有兩個(gè)參數(shù),一個(gè)是這個(gè)LDAPEntry的DN,一個(gè)是他的屬性集合,也就是LDAPAttributeSet。
最后,調(diào)用LDAPConnection實(shí)例化對(duì)象的add方法,把實(shí)體添加到服務(wù)器中。然后別忘了斷開(kāi)連接喔。整體的示例代碼如下:
java 代碼
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
attributeSet.add(new LDAPAttribute("objectclass", new String(
"inetOrgPerson")));
attributeSet.add(new LDAPAttribute("cn", new String[] { "李",
"Jim Smith", "Jimmy Smith" }));
attributeSet.add(new LDAPAttribute("givenname", new String[] { "測(cè)試",
"Jim", "Jimmy" }));
attributeSet.add(new LDAPAttribute("sn", new String("Smith")));
attributeSet.add(new LDAPAttribute("telephonenumber", new String(
"1 801 555 1212")));
attributeSet.add(new LDAPAttribute("mail",
new String("JSmith@Acme.com")));
attributeSet.add(new LDAPAttribute("userpassword", new String(
"newpassword")));
LDAPEntry entry = new LDAPEntry("cn=李,cn=Lizl,dc=excel,dc=com,dc=cn",
attributeSet);
LDAPConnection con = new LDAPConnection();
con.connect("6.1.19.154", 389);
con.bind(LDAPConnection.LDAP_V3, "cn=XXX", "XXXXXX");
con.add(entry);
System.out.println("成功的添加了一條記錄!");
con.disconnect();
10:27 | 永久鏈接 | 瀏覽 (1217) | 評(píng)論 (3) | 收藏 | ldap | 進(jìn)入論壇 |
永久鏈接
http://junewolf.javaeye.com/blog/52088
評(píng)論 共 3 條 發(fā)表評(píng)論
Hejrcc 2007-06-14 21:20
我覺(jué)得樓主的代碼很不好看啊,不要這么短就換行嘛。。。
還有, Indentation size = 4 就好了, 干嘛搞成8個(gè)字符這么寬啊,你這個(gè)代碼, 第2行好像不用空字符吧?
代碼
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
attributeSet.add(new LDAPAttribute("objectclass", new String("inetOrgPerson")));
attributeSet.add(new LDAPAttribute("cn", new String[] { "李", "Jim Smith", "Jimmy Smith" }));
attributeSet.add(new LDAPAttribute("givenname", new String[] { "測(cè)試", "Jim", "Jimmy" }));
attributeSet.add(new LDAPAttribute("sn", new String("Smith")));
attributeSet.add(new LDAPAttribute("telephonenumber", new String("1 801 555 1212")));
attributeSet.add(new LDAPAttribute("mail", new String("JSmith@Acme.com")));
attributeSet.add(new LDAPAttribute("userpassword", new String("newpassword")));
LDAPEntry entry = new LDAPEntry("cn=李,cn=Lizl,dc=excel,dc=com,dc=cn", attributeSet);
LDAPConnection con = new LDAPConnection();
con.connect("6.1.19.154", 389);
con.bind(LDAPConnection.LDAP_V3, "cn=XXX", "XXXXXX");
con.add(entry);
System.out.println("成功的添加了一條記錄!");
con.disconnect();
用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(四)
關(guān)鍵字: 用JAVA實(shí)現(xiàn)LDAP的訪問(wèn)(四)
這里來(lái)說(shuō)一說(shuō)怎么從LDAP中刪除一個(gè)實(shí)體。
首先,連接LDAP服務(wù)器,然后通過(guò)DN來(lái)刪除一個(gè)實(shí)體。
示例代碼如下:
java 代碼
LDAPConnection con = new LDAPConnection();
con.connect("6.1.19.154",389);
con.bind(LDAPConnection.LDAP_V3,"cn=XXXX","XXXXXX");
con.delete("cn=JSmith,dc=excel,dc=com,dc=cn");
System.out.println("成功刪除一條記錄!");
ITDS的infoCenter的地址
關(guān)鍵字: ITDS的infoCenter的地址
ITDS的infoCenter的地址:
http://publib.boulder.ibm.com/infocenter/tivihelp/v2r1/index.jsp?toc=/com.ibm.IBMDS.doc/toc.xml
可以通過(guò)ITDS的WEB管理工具對(duì)LDAP進(jìn)行管理,包括條目管理和objectclass以及
attributes的管理。
啟動(dòng)ITDS的WEB控制臺(tái)的方法是打開(kāi)cmd,
在D:\Program Files\IBM\LDAP\appsrv\bin文件夾下面(并不一定是這個(gè)路徑,看安裝時(shí)的位置,但\IBM\LDAP\appsrv\bin一般情況下不會(huì)變。)
輸入startServer server1。
訪問(wèn)Web控制臺(tái)的URL是:
http://6.1.9.54:9080/IDSWebApp/IDSjsp/IDSConsoleFrameWork.jsp
Ldap 的 Schema 中 的 objectclass 和 attributes 詳解
關(guān)鍵字: Ldap 的 Schema 中 的 objectclass 和 attributes 詳解
地址是:
http://www-03.ibm.com/servers/eserver/iseries/ldap/schema/