javax.net.ssl.SSLHandshakeException:java.security.cert.CertificateException:没有可用的X509TrustManager实现

时间:2014-03-17 22:52:56

标签: java spring ssl spring-boot

我的应用程序中有代码尝试使用带有指定证书的HTTPS连接到外部主机。

我的代码如下所示:

    //Load Two Keystores
    KeyStore keystore = KeyStore.getInstance("pkcs12", "SunJSSE");
    InputStream keystoreInput = new FileInputStream(cerPath);
    keystore.load(keystoreInput, passwd.toCharArray());
    System.out.println("Keystore has " + keystore.size() + " keys");

    // load the truststore, leave it null to rely on cacerts distributed with the JVM
    KeyStore truststore = KeyStore.getInstance("pkcs12", "SunJSSE");
    InputStream truststoreInput = new FileInputStream(cerPath);
    truststore.load(truststoreInput, passwd.toCharArray());
    System.out.println("Truststore has " + truststore.size() + " keys");

    //ssl context
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, null, null);
    SSLSocketFactory sf = new SSLSocketFactory(keystore, passwd);
    Scheme https = new Scheme("https", 443, sf);

    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(https);
    ...
    client = new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry));
    ...
    HttpResponse httpResponse = client.execute( targetHost, httpMethod);

变量cerPath包含用于建立连接的证书(xxxx.pfx)的路径。

在独立应用程序中,这可以很好地工作,但在我的spring-boot应用程序中,相同的代码会抛出此异常:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No X509TrustManager implementation available

我在普通的Spring应用程序中测试了这段代码,它也可以运行。

普通的SSL属性()也不起作用。

1 个答案:

答案 0 :(得分:0)

最后我们发现了这个问题。

我们在指向框架wiremock的pom.xml中有一个依赖项:

     <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>1.43</version>
        <exclusions>
             <exclusion>
              <groupId>org.mortbay.jetty</groupId>
              <artifactId>servlet-api</artifactId>
            </exclusion>
         </exclusions> 
    </dependency>

此框架在jar中包含一个产生此问题的/ keystore文件。当我们从pom中删除这种依赖时,一切正常。