I need to provide truststore and keystore when trying to connect to ldap sever using spring LdapTemplate. I can't import the certification using keytool or set by system property javax.net.ssl.trustStore
Now I config in following way:
<bean id="authenticationStrategy" class="org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy">
<property name="sslSocketFactory">
<bean class="com.sc.cops.common.auth.ssl.SslSocketFactoryBuilder">
<property name="trustStoreLocation" value="${oud.ldap.keyTrustLocation}" />
<property name="trustStorePassword" value="Password1" />
<!-- <property name="trustStorePassword">
<enc:decrypt key="${common.encryption.key}" cipher-text="${common.ssl.mq.keystore.password}" />
</property> -->
<property name="keyStoreLocation" value="${oud.ldap.keyTrustLocation}" />
<property name="keyStorePassword" value="Password1" />
</bean>
</property>
</bean>
<bean id="ldapContextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldaps://${ldap.server}:${ldap.port}/" />
<property name="userDn" value="${ldap.oud.userDn}" />
<property name="password" value="${ldap.oud.password}" />
<property name="authenticationStrategy" ref="authenticationStrategy"/>
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<property name="contextSource" ref="ldapContextSource" />
</bean>
In com.sc.cops.common.auth.ssl.SslSocketFactoryBuilder, we create SSLSocketFactory:
@Override
public SSLSocketFactory getObject() throws IOException, GeneralSecurityException {
TrustManagerFactory trustMgrFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore trustStore = loadKeyStore(getTrustStoreLocation(), getTrustStoreType(), getTrustStorePassword());
trustMgrFactory.init(trustStore);
KeyManagerFactory keyMgrFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore keyStore = loadKeyStore(getKeyStoreLocation(), getKeyStoreType(), getKeyStorePassword());
keyMgrFactory.init(keyStore, toCharArray(getKeyStorePassword()));
SSLContext sslContext = SSLContext.getInstance(SSL_VERSION);
sslContext.init(keyMgrFactory.getKeyManagers(), trustMgrFactory.getTrustManagers(), null);
return sslContext.getSocketFactory();
}
I debug the code, I can see the keystore and truststore are loaded correctly. And the jks file I provided is correct. But when I try to authenticate, I got error
Root exception is javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to requested target.