Java to Active Directory Ldap Java. Net. Socketexception: Connection or Outbound Has Closed
Good Evening, I Have Been Facing This Error for a Couple of Days by Now, and Despite Looking for a Solution All over the Web, I Coul'd Fix This Error. Import...
Good evening,
I have been facing this error for a couple of days by now, and despite looking for a solution all over the web, I coul'd fix this error.
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAPtest {
public static void main(String[] args) {
try {
String keystorePath = "C:/Program Files/Java/jdk-13.0.2/lib/security/cacerts";
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
Hashtable<String, String> ldapEnv = new Hashtable<>();
ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
ldapEnv.put(Context.PROVIDER_URL, "ldaps://localhost:10636");
ldapEnv.put(Context.SECURITY_AUTHENTICATION,"simple");
ldapEnv.put(Context.SECURITY_PRINCIPAL,"uid=admin,ou=system");
ldapEnv.put(Context.SECURITY_CREDENTIALS,"secret");
DirContext connection = new InitialDirContext(ldapEnv);
System.out.println("Benvenuto " + connection);
NamingEnumeration enm = connection.list("");
while (enm.hasMore()) {
System.out.println(enm.next());
}
enm.close();
connection.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
This code is actually working when SSL is not tested, replacing the
ldapEnv.put(Context.PROVIDER_URL, "ldaps://localhost:10636");
with
ldapEnv.put(Context.PROVIDER_URL, "ldap://localhost:10389");
I made the setup for the LDAP server with Apache Directory Studio, and followed this tutorial here in order to get the LDAPS to work:
So I made the certificate, even installed it and imported it with keytool into cacerts. I enabled portforwarding for the chosen port (10636), but still, I'm getting this exception:
javax.naming.CommunicationException: simple bind failed: localhost:10636 [Root exception is
java.net.SocketException: Connection or outbound has closed]
at (LdapClient.java:219)
at (LdapCtx.java:2795)
at (LdapCtx.java:320)
at (LdapCtxFactory.java:225)
at (LdapCtxFactory.java:189)
at (LdapCtxFactory.java:243)
at (LdapCtxFactory.java:154)
at (LdapCtxFactory.java:84)
at (NamingManager.java:730)
at (InitialContext.java:305)
at (InitialContext.java:236)
at (InitialContext.java:208)
at (InitialDirContext.java:130)
at Prova3.main(Prova3.java:31)
Caused by: java.net.SocketException: Connection or outbound has closed
at (SSLSocketImpl.java:1246)
at (BufferedOutputStream.java:81)
at (BufferedOutputStream.java:142)
at (Connection.java:398)
at (Connection.java:371)
at (LdapClient.java:359)
at (LdapClient.java:214)
... 13 more
Thank you in advance
1 Answer
For Googlers:
simple bind failed errors are almost always related to SSL connection.
With nc or telnet, check whether a connection can be established between client and remote host and port.
With SSLPoke.java (a simple Java class to check SSL connection), check whether certificates are correctly imported and used, also check correct TLS version. Use something like java -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2 -Djavax.net.debug=all SSLPoke google.com 443 > log.txt 2>&1.
Look for:
Warning: no suitable certificate found - continuing without client authentication= check whether you have setjavax.net.ssl.trustStoreFatal (HANDSHAKE_FAILURE): Couldn't kickstart handshaking= could be mismatched TLS versions- Also check whether your intermediate CA is expired