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)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 舞阳县| 商南县| 清苑县| 和林格尔县| 凌云县| 安达市| 南京市| 日照市| 莱阳市| 府谷县| 浮山县| 安图县| 金华市| 兰西县| 荔浦县| 应城市| 邹平县| 成都市| 哈尔滨市| 兰西县| 灵台县| 贺州市| 桐城市| 凌源市| 遂宁市| 宜都市| 伊宁市| 南华县| 高碑店市| 克什克腾旗| 施秉县| 珲春市| 巴青县| 绥中县| 金门县| 常德市| 刚察县| 民勤县| 策勒县| 宁晋县| 固镇县|