我正在尝试建立LDAPS连接,就像在unboundid文档here中所解释的那样
设置服务器
val config = new InMemoryDirectoryServerConfig("dc=org")
val sslUtilServer = new SSLUtil(new KeyStoreKeyManager("/path/mycert.pfx", "foo".toCharArray, "PKCS12", "server-cert"), null )
val listenerConfig = InMemoryListenerConfig.createLDAPSConfig("test", InetAddress.getByName("localhost"), 12345, sslUtilServer.createSSLServerSocketFactory(), null)
config.setListenerConfigs(listenerConfig)
config.addAdditionalBindCredentials("dn=foo", "bar")
val server = new InMemoryDirectoryServer(config)
server.startListening()
建立连接:
val c = server.getConnection("test")
c.connect("localhost", 12345)
c.bind("dn=foo", "bar")
在bind方法之后,我得到了这个异常:
com.unboundid.ldap.sdk.LDAPException: An error occurred while attempting to send the LDAP message to server localhost:12345: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.unboundid.ldap.sdk.LDAPConnectionInternals.sendMessage(LDAPConnectionInternals.java:556)
at com.unboundid.ldap.sdk.LDAPConnection.sendMessage(LDAPConnection.java:4254)
at com.unboundid.ldap.sdk.SimpleBindRequest.process(SimpleBindRequest.java:547)
at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2150)
at com.unboundid.ldap.sdk.LDAPConnection.bind(LDAPConnection.java:2095)
...
Cause: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1959)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1077)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:702)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:122)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at com.unboundid.ldap.sdk.LDAPConnectionInternals.sendMessage(LDAPConnectionInternals.java:525)
使用 TrustAllTrustManager 既没有解决问题:
val sslUtil = new SSLUtil(new TrustAllTrustManager())
val socketFactory = sslUtil.createSSLSocketFactory()
val ldap = new LDAPConnection(socketFactory)
ldap.connect("localhost", 12345)
ldap.bind("dn=foo", "bar")
有人可以帮忙吗?
编辑找到答案here