afunms

          My Software,My Dream—Forge a more perfect NMS product.

          java ldap (1)





          先用ldapbrowser測試下AD服務器,保證AD是可用的。
          以下是java代碼:

           

           

          package com.coreware.ems;

          import java.util.Hashtable;

          import javax.naming.Context;
          import javax.naming.NamingEnumeration;
          import javax.naming.NamingException;
          import javax.naming.directory.*;

          public class SimpleLDAPClient {
              
          public static void main(String[] args) {
                  Hashtable env 
          = new Hashtable();

                  env.put(Context.INITIAL_CONTEXT_FACTORY,
          "com.sun.jndi.ldap.LdapCtxFactory");
                  env.put(Context.PROVIDER_URL, 
          "ldap://192.168.80.130:389/dc=coreware,dc=com");
                  env.put(Context.SECURITY_AUTHENTICATION, 
          "simple");
                  env.put(Context.SECURITY_PRINCIPAL, 
          "cn=wangfm,cn=Users,dc=coreware,dc=com");
                  env.put(Context.SECURITY_CREDENTIALS, 
          "abc123!@#");
                  DirContext ctx 
          = null;
                  NamingEnumeration results 
          = null;
                  
          try {
                      ctx 
          = new InitialDirContext(env);
                      SearchControls controls 
          = new SearchControls();
                      controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                      results 
          = ctx.search("""(objectclass=person)", controls);
                      
                      
          while (results.hasMoreElements()) {
                          SearchResult searchResult 
          = (SearchResult) results.next();
                          Attributes attributes 
          = searchResult.getAttributes();
                          Attribute attr 
          = attributes.get("cn");
                          String cn 
          = (String) attr.get();
                          System.out.println(
          " Person Common Name = " + cn);
                      }

                  }
           catch (NamingException e) {
                      e.printStackTrace();
                  }
           finally {
                      
          if (results != null{
                          
          try {
                              results.close();
                          }
           catch (Exception e) {
                          }

                      }

                      
          if (ctx != null{
                          
          try {
                              ctx.close();
                          }
           catch (Exception e) {
                          }

                      }

                  }

              }

          }


          運行結果:
           Person Common Name = Administrator
           Person Common Name = Guest
           Person Common Name = afunms
           Person Common Name = db2admin
           Person Common Name = AFUNMS-WIN2008
           Person Common Name = krbtgt
           Person Common Name = wangfm

          posted on 2012-02-23 15:38 afunms 閱讀(332) 評論(0)  編輯  收藏


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


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 仪陇县| 姚安县| 资溪县| 宁强县| 梓潼县| 吉水县| 景洪市| 彰武县| 尼玛县| 潜江市| 长宁区| 台山市| 桂阳县| 双江| 伊宁县| 太原市| 会理县| 卢湾区| 宝应县| 临西县| 盖州市| 女性| 称多县| 镇康县| 兴义市| 安义县| 台南县| 中宁县| 武夷山市| 合作市| 玉山县| 乐至县| 青冈县| 双峰县| 呼和浩特市| 普洱| 黄浦区| 辉南县| 横峰县| 孝昌县| 绿春县|