Default SSL context init failed: null

后端 未结 1 802
名媛妹妹
名媛妹妹 2021-02-06 19:34

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 trusts

相关标签:
1条回答
  • 2021-02-06 20:03

    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 讨论(0)
提交回复
热议问题