4

我对以下代码有疑问...

System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStore","C:\\ClientKeyStore\\ClientKeyStore.p12");
System.setProperty("javax.net.ssl.trustStore","C:\\ClientKeyStore\\ClientKeyStore.keystore");
System.setProperty("javax.net.debug", "ssl");
System.setProperty("javax.net.ssl.keyStorePassword", "keystorepass");
System.setProperty("javax.net.ssl.trustStorePassword", "truststorepass");

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = new URL("https://url.com");
HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();
httpCon.setSSLSocketFactory(sslsocketfactory);
OutputStream out=httpCon.getOutputStream();

我尝试使用该System.setProperty(key,value)方法设置 SSL 上下文的信任库、密钥库和其他属性,但出现以下错误。

javax.net.ssl.SSLException: SSLSocketFactory is null. This can occur if javax.net.ssl.SSLSocketFactory.getDefault() is called to create a socket and javax.net.ssl.* properties are not set.

有人可以帮我解决这个问题。

4

2 回答 2

2

即使在 Windows 上,您也可以尝试对路径使用正斜杠:

System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStore","C:/ClientKeyStore/ClientKeyStore.p12");
System.setProperty("javax.net.ssl.trustStore","C:/ClientKeyStore/ClientKeyStore.keystore");
System.setProperty("javax.net.debug", "ssl");
System.setProperty("javax.net.ssl.keyStorePassword", "keystorepass");
System.setProperty("javax.net.ssl.trustStorePassword", "truststorepass");


SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();

这是你所有的代码吗?您的属性看起来不错,我能够使用您的示例创建一个套接字工厂。

于 2012-06-06T21:27:53.000 回答
0

我的 websphere 8.5 / ibm java 1.7 设置有这个问题。

在我的情况下,它是由 JREs <JRE_HOME>\lib\security\java.security 文件引起的。

就我而言,文件说...

ssl.KeyManagerFactory.algorithm=IbmX509
ssl.TrustManagerFactory.algorithm=PKIX

PKIX 值看起来很可疑,所以我将文件的这一行更改为...

ssl.TrustManagerFactory.algorithm=IbmX509

当我重新启动 websphere 服务器时,连接正常。

于 2018-11-05T14:52:12.043 回答