问题
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.
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.
回答1:
You can try forward slashes for the paths even on 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.
回答2:
I had this problem with my websphere 8.5 / ibm java 1.7 setup.
In my case it was caused by the JREs <JRE_HOME>\lib\security\java.security file.
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...
ssl.TrustManagerFactory.algorithm=IbmX509
When I restarted the websphere server the connection worked.
来源:https://stackoverflow.com/questions/10921941/javax-net-ssl-sslexception-sslsocketfactory-is-null