0

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

PatPanda
  • 3,644
  • 9
  • 58
  • 154
  • 1
    You don't set those in `application.properties`. What do you need this for? If they are for tomcat (for https) you can configure some tomcat specific properties. – M. Deinum Feb 02 '23 at 08:05
  • where is your file found? it seems as if it's either not recognized as a properties/yml file, or it is in the wrong location. And why did you go from truststore.jks to truststore.p12 ? the values shouldn't change just because you change the way of configuring them – Stultuske Feb 02 '23 at 08:05
  • Hey @Stultuske, typo, and the property file should be located correctly. all my other properties are taking effect, just not those javax.net.ssl. trustStore – PatPanda Feb 02 '23 at 08:34
  • key @M.Deinum, I understand we can set them in the code (as I did) or in environment properties, or when launching the jar. However, I am wondering if we can set those in application.properties, where literally all my other properties are (instead of yet another place). I need those to configure http clients from third party jar that are looking at those values – PatPanda Feb 02 '23 at 08:37
  • @PatPanda sure you can, using a work around. Whether it's the best way to do so, though, is open for debate. This question shows you one way: https://stackoverflow.com/questions/27724544/specifying-trust-store-information-in-spring-boot-application-properties – Stultuske Feb 02 '23 at 08:38
  • 1
    As I stated, you don't set those in `application.properties` you still would need something that exposes them as system properties. That is build in by default so you would have to build your own solution for that. Problem is if you want to load the properties you either have to make sure they execute early (maybe an `EnvironmentPostProcessor`) or some custom code that runs before that and loads the file itself (but that beats the purpose) or you find a different way to setup the 3rd party dependency (with explicitly setting the key-/truststore on it instead of relying on system properties). – M. Deinum Feb 02 '23 at 09:46

2 Answers2

1

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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
PatPanda
  • 3,644
  • 9
  • 58
  • 154