java javax.net.ssl.SSLException: SSLSocketFactory 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10921941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:05:24  来源:igfitidea点击:

javax.net.ssl.SSLException: SSLSocketFactory is null

javasslclient-certificates

提问by Bharath ABK

I have a problem with the following code...

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

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

I tried to set the trust store, keystore and other properties of SSL context using the System.setProperty(key,value)method, but I'm getting the following error.

我尝试使用该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.

Could someone please help me with this problem.

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

采纳答案by FGreg

You can try forward slashes for the paths even on windows:

即使在 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();

Is this all your code? Your properties look fine and I was able to use your example to create a socket factory.

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

回答by Michael Shaw

I had this problem with my websphere 8.5 / ibm java 1.7 setup.

我的 websphere 8.5 / ibm java 1.7 设置遇到了这个问题。

In my case it was caused by the JREs <JRE_HOME>\lib\security\java.security file.

就我而言,它是由 JRE <JRE_HOME>\lib\security\java.security 文件引起的。

In my case the file said...

在我的情况下,文件说......

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

The PKIX value looked suspicious so I changed this line of the file to...

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

ssl.TrustManagerFactory.algorithm=IbmX509

When I restarted the websphere server the connection worked.

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