简体   繁体   中英

Setting up keystore programmatically before getting default SSL context

I am in the impression that once I set system properties when I get SSLContext.getDefault() should return me SSLContext with those set properties. In the following case should be with specified keyStore. Unfortunately that's not what is happening. It falls back JVM's default keystore. Am I missing something ?

            System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks");
            System.setProperty("javax.net.ssl.keyStorePassword","changeit");
            System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");

            answer = SSLContext.getDefault();

I think by the time answer = SSLContext.getDefault(); was about to execute, SSLContext related classed were already loaded. I solved it by putting

System.setProperty("javax.net.ssl.keyStore", "/valida-location/keyStore.jks"); System.setProperty("javax.net.ssl.keyStorePassword","changeit"); in static block of my class. That way, there properties were set at the time of class loading. Thanks to @dave_thompson_085 for hint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM