【问题标题】:Spring LdapContextSource ignores SSLSocketFactorySpring LdapContextSource 忽略 SSLSocketFactory
【发布时间】:2018-05-25 05:51:06
【问题描述】:

Spring-Ldap 1.3.1

为了使用 TLS 测试 spring-ldap,我创建了一个 CustomSSLSocketFactory-class 接受所有证书(我知道这个安全问题)。

运行测试会导致

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

启用 SSL 调试后,会记录考虑使用标准信任库:

trustStore is: [path_to_jre]\cacerts
trustStore type is : jks
trustStore provider is : 
init truststore

这是执行测试的方式:

    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());  // <-- 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]");

对于另一个测试,我像这样扩展了 LdapContextSource:

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);
    }
}

握手比预期的效果好,但出现了另一个错误:

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)

所以问题是:如何正确设置 spring-ldap 以使其使用提供的 SSLSocketFactory?

【问题讨论】:

    标签: java spring ssl spring-ldap


    【解决方案1】:

    实际上由于 ldaps-URL 而失败。如果相应地设置了信任库,则会出现异常显示 TLS/SSL 已在运行,因此无法建立(因此无法组合 ldaps-URL 和 DefaultTlsDirContextAuthenticationStrategy)。

    StartTLS 是否在端口 389 上工作似乎也取决于目录。

    【讨论】:

    • 对于像我这样的傻瓜,解决办法是:切换协议 ldaps -> ldap。根据您的 LDAP 服务器设置,您可能还必须切换端口。如果您使用的是标准端口,请切换端口 636 -> 389。
    【解决方案2】:

    要修复错误,请使用 SimpleDirContextAuthenticationStrategy 而不是 DefaultTlsDirContextAuthenticationStrategy

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 1970-01-01
      • 2014-04-28
      • 2020-05-07
      • 2018-07-26
      • 1970-01-01
      • 2019-02-18
      • 2016-09-04
      • 2015-11-13
      相关资源
      最近更新 更多