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

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

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

    posts - 30,  comments - 85,  trackbacks - 0

     LDAP的英文全稱是Lightweight Directory Access Protocol,一般都簡稱為LDAP。它是基于X.500標準的,但是簡單多了并且可以根據需要定制。與X.500不同,LDAP支持TCP/IP,這對訪問Internet是必須的。LDAP的核心規范在RFC中都有定義,所有與LDAP相關的RFC都可以在LDAPman RFC網頁中找到。現在LDAP技術不僅發展得很快而且也是激動人心的。在企業范圍內實現LDAP可以讓運行在幾乎所有計算機平臺上的所有的應用程序從 LDAP目錄中獲取信息。LDAP目錄中可以存儲各種類型的數據:電子郵件地址、郵件路由信息、人力資源數據、公用密匙、聯系人列表,等等。通過把 LDAP目錄作為系統集成中的一個重要環節,可以簡化員工在企業內部查詢信息的步驟,甚至連主要的數據源都可以放在任何地方。

    以下是對ldap中進行連接,人員的增刪改查的過程。希望對初學者有一定的幫助。

    package net.risesoft.ldap;

    import java.util.Enumeration;
    import java.util.Hashtable;

    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.Attribute;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttribute;
    import javax.naming.directory.BasicAttributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.directory.ModificationItem;
    import javax.naming.directory.SearchControls;
    import javax.naming.directory.SearchResult;

    public class LdapTest {
     public static void main(String[] args) {
      String account = "admin";
      String password = "1";
      String root = "o=com"; // root

      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      env.put(Context.PROVIDER_URL, "ldap://localhost:389/" + root);
      env.put(Context.SECURITY_AUTHENTICATION, "simple");
      env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
      env.put(Context.SECURITY_CREDENTIALS, password);

      DirContext ctx = null;
      try {
       // 鏈接ldap
       ctx = new InitialDirContext(env);
       System.out.println("ldap認證成功");

       // 3.添加節點
       String newUserName = "user2";
       BasicAttributes attrsbu = new BasicAttributes();
       BasicAttribute objclassSet = new BasicAttribute("objectclass");
       objclassSet.add("person");
       objclassSet.add("top");
       objclassSet.add("organizationalPerson");
       objclassSet.add("inetOrgPerson");
       attrsbu.put(objclassSet);
       attrsbu.put("sn",   newUserName);
       attrsbu.put("uid",   newUserName);
       ctx.createSubcontext("cn=" + newUserName, attrsbu);

       // 5.修改節點
       account = "user2";
       String newDisplayName = "newDisplayName";
       ModificationItem modificationItem[] = new ModificationItem[1];
       modificationItem[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("displayName", newDisplayName));
       ctx.modifyAttributes("cn=" + account, modificationItem);

       // 查詢節點
       SearchControls constraints = new SearchControls();
       constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
       // constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
       NamingEnumeration en = ctx.search("", "cn=user2", constraints); // 查詢所有用戶
       while (en != null && en.hasMoreElements()) {
        Object obj = en.nextElement();
        if (obj instanceof SearchResult) {
         SearchResult si = (SearchResult) obj;
         System.out.println("name:   " + si.getName());
         Attributes attrs = si.getAttributes();
         if (attrs == null) {
          System.out.println("No   attributes");
         } else {
          for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements();) {
           Attribute attr = (Attribute) ae.next();
           String attrId = attr.getID();

           for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
            System.out.print(attrId + ":   ");
            Object o = vals.nextElement();
            if (o instanceof byte[])
             System.out.println();// new
                   // String((byte[])o)
            else
             System.out.println(o);
           }
          }
         }
        } else {
         System.out.println(obj);
        }
        System.out.println();
       }

       // 4.刪除節點
       account = "user2";
       ctx.destroySubcontext("cn=" + account);

      } catch (javax.naming.AuthenticationException e) {
       System.out.println("認證失敗");
      } catch (Exception e) {
       System.out.println("認證出錯:");
       e.printStackTrace();
      }

      if (ctx != null) {
       try {
        ctx.close();
       } catch (NamingException e) {
        // ignore
       }
      }
      System.exit(0);
     }
    }

    posted on 2007-05-31 14:25 安文豪 閱讀(6967) 評論(4)  編輯  收藏

    FeedBack:
    # re: 一個完整的ldap操作的例子
    2007-06-01 09:06 | popoer
    不知道樓主有沒有ldap排序和分頁的例子?  回復  更多評論
      
    # re: 一個完整的ldap操作的例子[未登錄]
    2008-07-31 12:27 | 過客
    Good  回復  更多評論
      
    # re: 一個完整的ldap操作的例子[未登錄]
    2011-08-02 13:51 | 海風
    多謝分享!  回復  更多評論
      
    # re: 一個完整的ldap操作的例子
    2012-12-17 18:24 |
    謝謝^_^_^  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    <2011年8月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(6)

    隨筆檔案(28)

    文章分類(3)

    文章檔案(4)

    最新隨筆

    搜索

    •  

    積分與排名

    • 積分 - 86469
    • 排名 - 666

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲性猛交xx乱| 亚洲高清资源在线观看| 亚洲国产精品久久久久秋霞小 | 亚洲国产精品第一区二区三区| 亚洲日本va一区二区三区| a毛片基地免费全部视频| 亚洲中文无码a∨在线观看| 日本片免费观看一区二区| 亚洲男女一区二区三区| 青娱乐免费在线视频| 中文字幕 亚洲 有码 在线 | 久久久久免费看黄a级试看| 亚洲国语精品自产拍在线观看| 黄色免费在线网站| 久久久久亚洲av无码专区导航| 亚洲一级毛片免费观看| 亚洲永久在线观看| 在线免费观看韩国a视频| 十八禁的黄污污免费网站| 亚洲国产婷婷六月丁香| 亚洲精品免费视频| 亚洲一区二区三区在线网站| 看全色黄大色大片免费久久| 羞羞漫画在线成人漫画阅读免费| 亚洲国产精品自产在线播放| a级精品九九九大片免费看| 亚洲国产精品久久人人爱| 成人免费视频一区| 一区二区三区免费视频观看| 无人视频在线观看免费播放影院| 全亚洲最新黄色特级网站| 亚洲娇小性xxxx色| 国产一精品一aⅴ一免费| 日本一区午夜艳熟免费| 亚洲同性男gay网站在线观看| 国产91久久久久久久免费| 青柠影视在线观看免费高清| 亚洲精品自拍视频| 亚洲国产精品无码久久青草| 91香蕉国产线观看免费全集| 亚洲激情在线观看|