javax.net.ssl.SSLException : SSLSocketFactory is null 异常

标签 java ssl client-certificates

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

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) 方法设置信任库、 keystore 和 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.

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

最佳答案

您甚至可以在 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();

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

关于javax.net.ssl.SSLException : SSLSocketFactory is null 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10921941/

相关文章:

python - 使用 pyopenssl 验证签名

PHP cURL 错误 (60) : SSL certificate issues: unable to get local issuer certificate

python - 使用 openssl、requests 和 wincertstore 获取客户端证书

iOS证书导入

ssl - IIS 10 和 HTTP/2 - 需要客户端证书

没有注释的 spring 中的 Java 对象验证

Java:将内容从一个变量移动到另一个变量

java - 去除字符串中空格的方法

java - PDF PrinterJob 获取作业状态

Apache - 无法启用 TLS 1.0 或 TLS 1.1