package service;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class AuthAd
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String userName = "aaa"; //用户名称
String password = "bbb"; //密码
String host = "192.168.40.17"; //AD服务器
String port = "389"; //端口
String domain = "@xxxx.xx"; //邮箱的后缀名
String url = new String("ldap://" + host + ":" + port);
String user = userName.indexOf(domain) > 0 ? userName : userName + domain;
Hashtable<String, String> env = new Hashtable<String, String>();
DirContext ctx;
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
try {
ctx = new InitialDirContext(env);
ctx.close();
System.out.println("验证成功!");
} catch (NamingException err) {
err.printStackTrace();
System.out.println("验证失败!");
}
}
}