1)先用hashTable初始化ldap用到的参数。
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://192.***.***.***:portNumber(389 is defualt)");
env.put(Context.SECURITY_PRINCIPAL,userID+"yourDomain(@***.com)");//
env.put(Context.SECURITY_CREDENTIALS, pass);
userID 和 pass就是登录域的账号。不要用全称。因为你在上面用到了域名。
2)开始验证。通过try..catch来判断用户的合法性。
try{
LdapContext ctx = new InitialLdapContext(env, null);
System.out.println("Succeess");
} catch (Exception e) {
if (e.getMessage().indexOf("775") > = 0) {
System.out.println("你的账号被锁了。");
}else {
System.out.println("Invalid User");
}
}