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 閱讀(327) 評論(0)  編輯  收藏


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


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 景泰县| 连城县| 若尔盖县| 安塞县| 赤峰市| 黄骅市| 清水县| 莒南县| 襄城县| 桂阳县| 山阳县| 通化市| 蒙城县| 龙口市| 阿坝县| 澄江县| 九龙坡区| 师宗县| 高要市| 南川市| 应城市| 东宁县| 土默特右旗| 安平县| 静宁县| 南部县| 安远县| 顺义区| 平远县| 民和| 咸宁市| 买车| 新乡县| 广河县| 孟州市| 施秉县| 崇信县| 东乌珠穆沁旗| 嘉兴市| 虞城县| 沅陵县|