posts - 30,  comments - 85,  trackbacks - 0

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

          以下是對(duì)ldap中進(jìn)行連接,人員的增刪改查的過(guò)程。希望對(duì)初學(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認(rèn)證成功");

             // 3.添加節(jié)點(diǎn)
             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é)點(diǎn)
             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);

             // 查詢(xún)節(jié)點(diǎn)
             SearchControls constraints = new SearchControls();
             constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
             // constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
             NamingEnumeration en = ctx.search("", "cn=user2", constraints); // 查詢(xún)所有用戶(hù)
             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é)點(diǎn)
             account = "user2";
             ctx.destroySubcontext("cn=" + account);

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

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

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

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


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

          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(6)

          隨筆檔案(28)

          文章分類(lèi)(3)

          文章檔案(4)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 86706
          • 排名 - 670

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 清镇市| 屏南县| 刚察县| 贵溪市| 内乡县| 五河县| 乌鲁木齐市| 苏尼特右旗| 循化| 崇礼县| 宁陕县| 和田县| 浮梁县| 佛山市| 元朗区| 南昌市| 灌南县| 泰顺县| 大丰市| 蛟河市| 新源县| 白水县| 新郑市| 临澧县| 肇州县| 林口县| 察雅县| 岗巴县| 平昌县| 百色市| 石城县| 德令哈市| 萨嘎县| 富阳市| 宁强县| 榆社县| 兴业县| 贡山| 汾阳市| 界首市| 蕉岭县|