1

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

6
  • 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. Commented Feb 2, 2023 at 8: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 Commented Feb 2, 2023 at 8: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 Commented Feb 2, 2023 at 8: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 Commented Feb 2, 2023 at 8:37
  • 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). Commented Feb 2, 2023 at 9:46

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.