LDAP SSL连接出错
代码如下
public void init() {
Hashtable env = new Hashtable();
//String keystore=System.getProperty("user.dir")+ "\\cacerts";//获取AD证书
//String keystore="C:\\Program Files\\Java\\jdk1.8.0_111\\jre\\lib\\security\\cacerts";
String keystore="C:\\Users\\Shmily\\cacerts";
System.setProperty("javax.net.ssl.trustStore", keystore);
SSLContext sslContext = null;
//System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
String LDAP_URL = "ldap://192.168.2.12:636"; // LDAP访问地址
String adminName = "tianchuang\\administrator"; // 注意用户名的写法:domain\User或
//String adminName = "topscore\\admin";
String adminPassword = "topscore"; // 密码
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_PROTOCOL, "ssl");//链接认证服务器
env.put(Context.PROVIDER_URL, LDAP_URL);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, adminName);
env.put(Context.SECURITY_CREDENTIALS, adminPassword);
//env.put(Context.REFERRAL,"ignore");
//env.put("java.naming.ldap.factory.socket", "com.auth.ldaps.DummySSLSocketFactory");
try {
dc = new InitialDirContext(env);// 初始化上下文
System.out.println("认证成功");// 这里可以改成异常抛出。
} catch (javax.naming.AuthenticationException e) {
System.out.println("认证失败");
} catch (Exception e) {
System.out.println("认证出错:" + e);
}
}
用默认端口389可以正常连接,进行操作,但是不能修改密码且创建的用户是没有密码 ps:这就没有意义了
我已经将AD域的证书导入到JDK中的证书库中了,但是连接的时候报错
认证出错:javax.naming.CommunicationException: simple bind failed: 192.168.2.12:636 [Root exception is javax.net.ssl.SSLException: java.net.SocketException: Connection reset]
java.lang.NullPointerException
at com.agile.ad.AdServiceImpl.add(AdServiceImpl.java:135)
at com.agile.ad.AdServiceImpl.<init>(AdServiceImpl.java:43)
at com.agile.ad.AdServiceImpl.main(AdServiceImpl.java:405)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
java.lang.NullPointerException
at com.agile.ad.AdServiceImpl.Ldapbyuserinfo(AdServiceImpl.java:350)
at com.agile.ad.AdServiceImpl.<init>(AdServiceImpl.java:50)
at com.agile.ad.AdServiceImpl.main(AdServiceImpl.java:405)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Throw Exception : java.lang.NullPointerException
Process finished with exit code 0