Search code examples
javaldapunboundid-ldap-sdk

UnboundID, LDAP jdk migration


How can I call a SSL connection on a:

com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPConnection; ?

The following:

SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager());
LDAPConnection ldp = new LDAPConnection(sslUtil.createSSLSocketFactory(), getHost(), getPort(), getAuthid(), getAuthpw());  

only works with:

import com.unboundid.ldap.sdk.LDAPConnection;

However, I would like to stick to the migrated ldapjdk connection, if at all possible.

Thank you,


Solution

  • When using a com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPConnection, one of the constructors allows you to specify a com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPSocketFactory instance that will be used to create the underlying sockets. Further, you can use the com.unboundid.ldap.sdk.migrate.ldapjdk.JavaToLDAPSocketFactory class as an LDAPSocketFactory that wraps a javax.net.SocketFactory (of which javax.net.ssl.SSLSocketFactory is a subclass).

    The code to do this should be something like:

     SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager());
     SSLSocketFactory sslSocketFactory = sslUtil.createSSLSocketFactory();
     JavaToLDAPSocketFactory ldapSocketFactory = 
          new JavaToLDAPSocketFactory(sslSocketFactory);
     LDAPConnection ldp = new LDAPConnection(ldapSocketFactory);
    

    Note that for code you actually intend to use in real-world applications, you should probably use a better trust manager than one that blindly trusts any certificate presented by the server, since the TrustAllTrustManager doesn't do anything to help prevent man-in-the-middle attacks. But the TrustAllTrustManager is a convenient first step to verify that you can get secure communication working before switching to some strong validation with something like the TrustStoreTrustManager.

    Neil