<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    空間站

    北極心空

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    用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/

    posted on 2007-07-13 11:00 蘆葦 閱讀(19341) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): 數(shù)據(jù)庫(kù)JAVA

    Feedback

    # re: 用JAVA實(shí)現(xiàn)LDAP的訪問(wèn) 2013-06-05 10:10 www.dsprint.cn
    不錯(cuò)  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 夜夜爽妓女8888视频免费观看| 国产成人免费ā片在线观看| 九九久久国产精品免费热6| 亚洲欧洲校园自拍都市| 国产v亚洲v天堂无码网站| 免费国产精品视频| 成年女人毛片免费播放人| 日本免费一区二区三区| 中文在线免费观看| 四虎一区二区成人免费影院网址| 亚洲日韩一区二区一无码| 91亚洲视频在线观看| 久久亚洲精品中文字幕无码| 亚洲一级特黄大片无码毛片| 国产精品免费看香蕉| 免费观看的av毛片的网站| 中国在线观看免费国语版| 24小时日本电影免费看| 免费国产污网站在线观看15 | 国产精品久久久久免费a∨ | 久久精品亚洲中文字幕无码网站| 亚洲中文字幕伊人久久无码| 免费又黄又爽又猛的毛片| 日本a级片免费看| 国产精品嫩草影院免费| 女人18毛片水真多免费看| 成人性生交视频免费观看| 成人无码区免费视频观看 | 亚洲人成网站在线观看播放动漫 | 国产亚洲精彩视频| 国产亚洲综合久久| 特级毛片全部免费播放a一级 | 四虎www成人影院免费观看| 成人一a毛片免费视频| 最好免费观看韩国+日本| 全免费一级毛片在线播放| 日韩高清免费观看| 又粗又大又猛又爽免费视频| 亚洲精品乱码久久久久久蜜桃 | 亚洲日韩国产AV无码无码精品| 亚洲高清一区二区三区|