81,092
社区成员
发帖
与我相关
我的任务
分享
public static boolean checkLdap2(String uid, String pwd) {
int totalResults = 0;
Hashtable env = new Hashtable();
String address = Constants.ldapaddress;
String keystore = Constants.keystore;
String searchBase = Constants.searchBase;
String ip = Constants.ip;
System.out.println("ldapaddress = [" + address + "]");
System.out.println("searchBase = [" + searchBase + "]");
System.out.println("ip = [" + ip + "]");
System.out.println("keystore = [" + keystore + "]");
String adminName = uid + "@" + address;
String adminPassword = pwd;
String ldapURL = "ldaps://" + ip;
System.setProperty("javax.net.ssl.trustStore", keystore);
env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
env.put("java.naming.security.authentication", "simple");
env.put("java.naming.security.principal", adminName);
env.put("java.naming.security.credentials", adminPassword);
env.put("java.naming.security.protocol", "ssl");
env.put("java.naming.provider.url", ldapURL);
DirContext ctx = null;
try {
ctx = new InitialLdapContext(env, null);
log.info("认证成功");
System.out.println("认证成功");
} catch (AuthenticationException e) {
log.error("认证失败", e);
} catch (Exception e) {
log.error("认证出错:", e);
}
if (ctx != null) {
try {
ctx.close();
return true;
} catch (NamingException localNamingException) {
log.error("认证异常", localNamingException);
}
}
return false;
}