1. public static boolean adConnect(String username, String password) {  
  2.             Hashtable HashEnv = new Hashtable();  
  3.               
  4.             String LDAP_URL = "ldap://" + AD_SERVER + ":389"; // LDAP访问地址  
  5.             String adminName = username + "@" + AD_NAME; // 注意用户名的写法:domain\User 或   User@huarui.com  
  6.             String adminPassword = password; // 密码  
  7.             HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别  
  8.             HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User  
  9.             HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password  
  10.             HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,  
  11.                     "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类  
  12.             HashEnv.put(Context.PROVIDER_URL, LDAP_URL);  
  13.  
  14.             try {  
  15.                 DirContext ctx = new InitialDirContext(HashEnv);  
  16.             } catch (CommunicationException e1) {  
  17.                 return false;  
  18.             } catch (Exception e2) {  
  19.                 return false;  
  20.             }  
  21.             return true;  
  22.         }