- public static boolean adConnect(String username, String password) {
- Hashtable HashEnv = new Hashtable();
- String LDAP_URL = "ldap://" + AD_SERVER + ":389"; // LDAP访问地址
- String adminName = username + "@" + AD_NAME; // 注意用户名的写法:domain\User 或 User@huarui.com
- String adminPassword = password; // 密码
- HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
- HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
- HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password
- HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
- "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
- HashEnv.put(Context.PROVIDER_URL, LDAP_URL);
- try {
- DirContext ctx = new InitialDirContext(HashEnv);
- } catch (CommunicationException e1) {
- return false;
- } catch (Exception e2) {
- return false;
- }
- return true;
- }