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

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

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

    空間站

    北極心空

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

    用JAVA實(shí)現(xiàn)LDAP的訪問(一)
    關(guān)鍵字:   用JAVA實(shí)現(xiàn)LDAP的訪問(一)    
     LDAP現(xiàn)在用的越來越多,所謂LDAP既Lightweight Directory Access Protocol。關(guān)于它的一些基本知識(shí),我在這里就不做系統(tǒng)的介紹了,網(wǎng)上有很多的資料。我主要說一下在JAVA的語言環(huán)境中,怎樣來操作LDAP。
        在這里,我推薦兩個(gè)工具:LDAPTemplate和JLDAP。
        網(wǎng)上的資料比較少,而且不少都是E文的,可能英語不太好的朋友,就很難入門了。在這我把我的經(jīng)驗(yàn)總結(jié)一下,和大家分享。
        LDAPTemplate是基于Spring1.2.7來開發(fā)的,其用法和Spring的JDBCTemplate差不多。最初,我是用這個(gè)開源的框架來對(duì)LDAP進(jìn)行操作的,但是后來由于開發(fā)工具的轉(zhuǎn)變,由eclipse轉(zhuǎn)到了RAD上,而RAD所用的JDK卻不支持Spring1.2.7(看來網(wǎng)上的謠傳沒錯(cuò),IBM總在某個(gè)陰暗的角落在和SUN作對(duì)),沒辦法,只好另辟蹊徑。后來發(fā)現(xiàn)了JLDAP,經(jīng)過一番研究,發(fā)現(xiàn)它用起來并不比LDAPTemplate復(fù)雜,但在對(duì)象持久化方面需要自己去做,而LDAPTemplate通過AttributeMappers就可以把查詢到的結(jié)果轉(zhuǎn)換成POJO了。

    用JAVA實(shí)現(xiàn)LDAP的訪問(二)
    關(guān)鍵字:   用JAVA實(shí)現(xiàn)LDAP的訪問(二)    
     下面來具體的說一下怎么用JLDAP。首先要去下載一下JLDAP,具體下載的地址可以上網(wǎng)去搜。下載下來以后,lib里面的是開發(fā)所要用到的包,doc里面是幫助文檔API和示例程序。
        先說說怎么查詢,其實(shí)查詢非常的簡單,如果用過JDBC連數(shù)據(jù)庫的話,那么連LDAP相比起來更加的簡單。
        首先建立一個(gè)LDAPConnection對(duì)象。這個(gè)對(duì)象也可以通過連接池PoolManager來獲得。LDAPConnection con = new LDAPConnection();然后運(yùn)行connect方法和bind方法。連接上LDAP以后,就可以通過search方法來查找數(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ì), 我也剛剛開始學(xué)。
    我寫了個(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
    看見還有一種寫法用來獲取 DirContext, 下面的寫法指定了 INITIAL_CONTEXT_FACTORY屬性,我想知道我前面一種寫法里面, env.put(Context.INITIAL_CONTEXT_FACTORY, ?);
    這個(gè)INITIAL_CONTEXT_FACTORY 我沒有設(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的訪問(三)
    關(guān)鍵字:   用JAVA實(shí)現(xiàn)LDAP的訪問(三)    
      雖然LDAP主要是用來進(jìn)行讀操作的,但不可避免的,我們也要向其中添加一些新的數(shù)據(jù)。用JLDAP向LDAP服務(wù)器添加數(shù)據(jù)的操作也是非常簡單的。
       為什么說非常簡單呢,因?yàn)榇篌w上也就是分三步。第一步,連接LDAP服務(wù)器。第二步,建立一個(gè)要添加的新的實(shí)體LDAPEntry,并添加相應(yīng)的屬性。第三步,通過add方法向LDAP中添加實(shí)體。
       首先說連接服務(wù)器。還是非常簡單的三步:
    java 代碼
    LDAPConnection con = new LDAPConnection();   
     con.connect("hostname",hostport);   
     con.bind("version","DN","password");  

    連接后,可以建實(shí)體了,也就相當(dāng)與為數(shù)據(jù)庫添加一條新的記錄。這里用到了幾個(gè)類: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ù)器中。然后別忘了斷開連接喔。整體的示例代碼如下:
    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
    我覺得樓主的代碼很不好看啊,不要這么短就換行嘛。。。
    還有, 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的訪問(四)
    關(guān)鍵字:   用JAVA實(shí)現(xiàn)LDAP的訪問(四)    
    這里來說一說怎么從LDAP中刪除一個(gè)實(shí)體。
    首先,連接LDAP服務(wù)器,然后通過DN來刪除一個(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
     
    可以通過ITDS的WEB管理工具對(duì)LDAP進(jìn)行管理,包括條目管理和objectclass以及
    attributes的管理。
    啟動(dòng)ITDS的WEB控制臺(tái)的方法是打開cmd,
    在D:\Program Files\IBM\LDAP\appsrv\bin文件夾下面(并不一定是這個(gè)路徑,看安裝時(shí)的位置,但\IBM\LDAP\appsrv\bin一般情況下不會(huì)變。)
    輸入startServer server1。
     
    訪問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 蘆葦 閱讀(19353) 評(píng)論(1)  編輯  收藏 所屬分類: 數(shù)據(jù)庫 、JAVA

    Feedback

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

    主站蜘蛛池模板: 亚洲精品成人网站在线播放| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 国产美女在线精品免费观看| a国产成人免费视频| 成人午夜性A级毛片免费| 精品一区二区三区免费| 精品亚洲国产成AV人片传媒| 国产大片线上免费观看| 日本一区午夜艳熟免费| 全黄A免费一级毛片| 亚洲成_人网站图片| 久久亚洲美女精品国产精品| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 亚洲另类无码专区丝袜| 亚洲视频免费一区| 国产精品免费看久久久无码| 一级毛片高清免费播放| 亚洲成a人无码亚洲成av无码| 亚洲女人初试黑人巨高清| 久久久久久久尹人综合网亚洲| 最近2019年免费中文字幕高清 | 99热这里只有精品免费播放| 男男AV纯肉无码免费播放无码| 四虎影视成人永久免费观看视频 | 久久久久久久免费视频| 亚洲av片在线观看| 亚洲精品乱码久久久久久中文字幕| 永久免费在线观看视频| 久久99热精品免费观看动漫| 国产午夜成人免费看片无遮挡 | 在线看片人成视频免费无遮挡| 2021久久精品免费观看| 国产又黄又爽又猛免费app| 97免费人妻无码视频| 免费福利在线播放| 手机看黄av免费网址| 国产无人区码卡二卡三卡免费 | 伊人久久综在合线亚洲91| 国产成人精品久久亚洲| 国产成人免费高清激情明星| 亚洲黄色片免费看|