Recursively change system property at runtime in java

随声附和 提交于 2019-12-21 21:25:14

问题


I am having a question and searching for an example for changing system property at runtime in java. In other words , I am having a standalone library which will load System.setProperty("javax.net.ssl.trustStore", trustStorePath) where the value of trustStorePath will change according to condition. If condition changes then I need to change the value of trustStorePath and need to set System Property.

But the story is when I set the value for very first time, it stores the value and use it even if I change the value of trustStorePath and again set system property. The change did not reflect.

So , How can I do the same. Below is the sample code snippet for the same .

        if (getFile(keyStorePath).exists()  && isChanged ) {
                System.setProperty("javax.net.ssl.keyStore", keyStorePath);
                System.setProperty("javax.net.ssl.keyStoreType", "JKS");
                System.setProperty("javax.net.ssl.keyStorePassword", Pwd);
        }else if (getFile(testMerchantKeyStorePath).exists() ) {
            System.setProperty("javax.net.ssl.keyStore", testMerchantKeyStorePath);
                System.setProperty("javax.net.ssl.keyStoreType", "JKS");
                System.setProperty("javax.net.ssl.keyStorePassword",Pwd);

    }

回答1:


Sounds like you want to use a dynamic trust store. You could do this before you open any connection:

    KeyStore ts = KeyStore.getInstance("JKS");
    ts.load(new FileInputStream(new File("Your_New_Trust_Store_Path")), "password".toCharArray());

    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    tmf.init(ts);

    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tmf.getTrustManagers(), null);

    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

    // Open Connection .... etc. ....

You could do this each time your trustStorePath changes.



来源:https://stackoverflow.com/questions/35125154/recursively-change-system-property-at-runtime-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!