title: Spring LdapContextSource ignores SSLSocketFactory
body: <p>Spring-Ldap 1.3.1</p>
  
  <p>For testing spring-ldap with TLS I have created a CustomSSLSocketFactory-class accepting all certificates (I know about the security-issue with this).</p>
  
  <p>Running a test however results in</p>
  
  <pre><code>PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  </code></pre>
  
  <p>With SSL-debug enabled, it is logged that the standard-truststore is considered:</p>
  
  <pre><code>trustStore is: [path_to_jre]\cacerts
  trustStore type is : jks
  trustStore provider is : 
  init truststore
  </code></pre>
  
  <p>This is how the test is performed:</p>
  
  <pre><code>    LdapContextSource lcs = new LdapContextSource();
  
      lcs.setBase("[base]");
      lcs.setUserDn("[userDn]");
      lcs.setPassword("[password]");
      lcs.setPooled(false);
      lcs.setUrl("ldaps://[server-address]:636");
  
      DefaultTlsDirContextAuthenticationStrategy strategy = new DefaultTlsDirContextAuthenticationStrategy();
      strategy.setShutdownTlsGracefully(true);
      strategy.setSslSocketFactory(new CustomSSLSocketFactory());  // &lt;-- not considered at all
      strategy.setHostnameVerifier(new HostnameVerifier(){
  
          @Override
          public boolean verify(String hostname, SSLSession session){
  
              return true;
          }
      });
  
      lcs.setAuthenticationStrategy(strategy);
      lcs.afterPropertiesSet();
      lcs.getContext("[principal]", "[credential]");
  </code></pre>
  
  <p>For another test, I extended LdapContextSource like this:</p>
  
  <pre><code>public class ExtLdapContextSource extends LdapContextSource{
  
      public DirContext getContext(String principal, String credentials) {
  
          getAnonymousEnv().put("java.naming.security.protocol", "ssl");
          getAnonymousEnv().put("java.naming.ldap.factory.socket", "[package].CustomSSLSocketFactory");
          return super.getContext(principal, credentials);
      }
  }
  </code></pre>
  
  <p>The handshake than works as expected, but another error shows up:</p>
  
  <pre><code>javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: DSID-0C090DF2, comment: TLS or SSL already in effect, data 0, v1db1
  at com.sun.jndi.ldap.LdapCtx.mapErrorCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.extendedOperation(Unknown Source)
  at javax.naming.ldap.InitialLdapContext.extendedOperation(Unknown Source)
  at org.springframework.ldap.core.support.AbstractTlsDirContextAuthenticationStrategy.processContextAfterCreation(AbstractTlsDirContextAuthenticationStrategy.java:133)
  at org.springframework.ldap.core.support.AbstractContextSource.getContext(AbstractContextSource.java:109)
  </code></pre>
  
  <p>So the question is: How to setup spring-ldap correctly in order to make it use the provided SSLSocketFactory?</p>