【发布时间】:2014-04-23 09:48:56
【问题描述】:
我的应用程序中有代码尝试使用带有指定证书的 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 属性 () 也不起作用。
【问题讨论】:
-
不确定为什么要加载密钥库和信任库,因为在
sslcontext.init(...)中显然根本没有使用它们。 (也许只在您的 Apache HTTP 客户端SSLSocketFactory中,它似乎也没有使用SSLContext。) -
是的,我想是的。但是JVM加载了wiremock的keystore,不知道为什么。
标签: java spring ssl spring-boot