开发者

Default SSL context init failed: null

When creating a SSL server, I got this exception: Default SSL context init failed: null. It seems that it comes from the fact it can't find the keystore and truststore. I try to set them from a jar file. The file exists in the jar but it seems that the resource cannot be found.

String keystore = TestFramework.class.getResource("/security/keystore.jks").getFile();
System.setProperty("javax.net.ssl.keyStore", keystore);
System.setProperty("javax.net.ssl.key开发者_JAVA技巧StorePassword", password);
String truststore = TestFramework.class.getResource("/security/truststore").getFile();
System.setProperty("javax.net.ssl.trustStore", truststore);
System.setProperty("javax.net.ssl.trustStorePassword", "ebxmlrr");

I ran a ls command to check if the file exists. It exists. Then I check if the keystore.jks exists by running the command jar -tf myjar.jar | grep security and it exists.

security/
security/keystore.jks
security/truststore

My application is running under Tomcat.


The reason that won't work is that those system properties are expecting a file on the actual filesystem, not from within an archive. You'd be better off creating your own SSLContext using those keystore and trustore streams:

KeyStore keyStore = KeyStore.getInstance("jks");
keyStore.load(TestFramework.class.getResourceAsStream("/security/keystore.jks"), password.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyManagerFactory.init(keyStore, password.toCharArray());

KeyStore trustStore = KeyStore.getInstance("jks");
trustStore.load(TestFramework.class.getResourceAsStream("/security/truststore"), "ebxmlrr".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
trustManagerFactory.init(trustStore);

SSLContext context = SSLContext.getInstance("SSL");
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());

...

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜