Home > Back-end >  Java SpringBoot: application.properties for javax.net.ssl. trustStore trustStorePassword keyStore ke
Java SpringBoot: application.properties for javax.net.ssl. trustStore trustStorePassword keyStore ke

Time:02-03

Small question regarding Java SpringBoot application.properties please.

I have a simple SpringBoot web application, and during the business flow, it needs the javax.net.ssl. trustStore trustStorePassword keyStore keyStorePassword to be set.

Therefore, in the main, this is what I tried:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        System.setProperty("javax.net.ssl.trustStore", "truststore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
        System.setProperty("javax.net.ssl.keyStore", "keystore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
        SpringApplication.run(MyApplication.class, args);
    }

}

And I verified, this works fine. (removing the property from here will cause my app to fail) Therefore, I would believe those properties works.

I understand I can pass those as command line, but I would like to use application.properties, where all my other properties are.

Now, I would like to externalize those properties into the application.properties / yml please.

What I tried:

server.port=8080
javax.net.ssl.trustStore=truststore.jks
javax.net.ssl.trustStorePassword=changeit
javax.net.ssl.keyStore=keystore.jks
javax.net.ssl.keyStorePassword=changeit

However, those properties are not taken account, I am getting the same behaviour as if I did not set anything.

May I ask, how to set those system properties in application.properties please?

Thank you

CodePudding user response:

The properties file is parsed after SpringApplication.run is executed, and are therefore runtime arguments, not available as System properties.

As linked in the comments, you need some PostConstruct hook to execute System.setProperty after Spring properties have loaded

You can still use external files such as .env (plus shell plugins to load this file automatically) to map environment variables, such as JAVA_TOOL_OPTIONS with any system properties you need, it just can't be application.properties

CodePudding user response:

Based on:

@SpringBootApplication
public class MyApplication  {

    @Value("${my.property.ssl.trustStore}")
    private String trustStore;
    @Value("${my.property.ssl.trustStorePassword}")
    private String trustStorePassword;
    @Value("${my.property.ssl.keyStore}")
    private String keyStore;
    @Value("${my.property.ssl.keyStorePassword}")
    private String keyStorePassword;

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @PostConstruct
    void postConstruct() {
        System.setProperty("javax.net.ssl.trustStore", trustStore);
        System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
        System.setProperty("javax.net.ssl.keyStore", keyStore);
        System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
    }

}

Will pick up from application.properties:

my.property.ssl.trustStore=truststore.jks
my.property.ssl.trustStorePassword=changeit
my.property.ssl.keyStore=keystore.jks
my.property.ssl.keyStorePassword=changeit
  • Related