How to set system variable from Spring Cloud Config Server

时间:2016-11-12 05:56:29

标签: java spring ssl config

I'm searching for a way to set system variables (e.g. -Djavax.net.ssl.keyStore) from Spring Cloud Config Server, because I'm facing exactly this behaviour (https://stackoverflow.com/a/30199253/1406669) in a mutual ssl environment.
There are various ways to set it statically (https://stackoverflow.com/a/36895827/1406669 || https://gist.github.com/unamanic/a7eb0c17b78fb03617cc955b06285b1d).
The thing I don't like about it, is setting it statically (once set on startup and never refreshed) and the need to define the keys in a static way. In this way I would have to redeploy the apps when there would be the need to introduce a new system variable. This is a the thing I try to avoid.
Does anybody got an idea?

1 个答案:

答案 0 :(得分:1)

刷新事件通过ApplicationChangeEvent工作,您的应用程序也可以通过实现ApplicationListener来监听。

https://github.com/spring-cloud/spring-cloud-commons/blob/master/docs/src/main/asciidoc/spring-cloud-commons.adoc#environment-changes

@Component
public class DynamicSystemProperties implements ApplicationListener<EnvironmentChangeEvent>{

    private final Environment env;

    @Autowired
    public DynamicSystemProperties(Environment env) {
        this.env = env;

    }

    @Override
    public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) {

        if(env.containsProperty("system.javax.net.ssl.keyStore")) {
            String keystore = env.getProperty("system.javax.net.ssl.keyStore");
            System.out.println("system.javax.net.ssl.keyStore - " + keystore);

            System.getProperties().setProperty("javax.net.ssl.keyStore", keystore);
        }
    }
}