我的应用程序中有代码,尝试使用具有指定证书的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属性()也不起作用。
最佳答案
最后,我们发现了问题。
我们在pom.xml中有一个依赖项,指向框架wiremock:
<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中删除此依赖项时,一切正常。