posts - 30,  comments - 85,  trackbacks - 0

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

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

          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.添加節(jié)點
             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.修改節(jié)點
             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);

             // 查詢節(jié)點
             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.刪除節(jié)點
             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 安文豪 閱讀(6972) 評論(4)  編輯  收藏

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

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


          網(wǎng)站導(dǎo)航:
           

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(6)

          隨筆檔案(28)

          文章分類(3)

          文章檔案(4)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 86628
          • 排名 - 668

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 商城县| 钟山县| 兴隆县| 航空| 余干县| 绍兴县| 清流县| 高阳县| 公主岭市| 高雄县| 方正县| 大邑县| 宁陕县| 库尔勒市| 嵊泗县| 东山县| 杭锦旗| 开原市| 西城区| 宜兴市| 博客| 中西区| 吴川市| 涟水县| 阿克苏市| 和顺县| 武鸣县| 肥城市| 清徐县| 荔波县| 汶上县| 西贡区| 七台河市| 突泉县| 安岳县| 新昌县| 茶陵县| 延吉市| 浦江县| 宁阳县| 长沙县|