Process Spring Boot externalized property values

后端 未结 5 712
广开言路
广开言路 2020-12-03 12:22

I have the task of obfuscating passwords in our configuration files. While I don\'t think this is the right approach, managers disagree...

So the project I am workin

5条回答
  •  Happy的楠姐
    2020-12-03 13:01

    I used @Daniele Torino's answer and made several minor changes.

    First, thanks to his link to the options on how to make spring recognize Initializer, I chose to do it in the Application:

    public static void main(String[] args) throws Exception {
        SpringApplication application=new SpringApplication(Application.class);
        application.addInitializers(new PropertyPasswordDecodingContextInitializer());
        application.run(args);
    }
    

    Second, IDEA told me that that else if (source instanceof CompositePropertySource) { is redundant and it is because CompositePropertySource inherits from EnumerablePropertySource.

    Third, I beleive there is a minor bug: it messes up the order of property resolution. If you have one encoded property in environment, and another one in application.properties file the environment value will be overwritten with the application.properties value. I changed the logic to insert the decodedProperties right before encoded:

            for (PropertySource propertySource : environment.getPropertySources()) {
                    Map propertyOverrides = new LinkedHashMap<>();
                    decodePasswords(propertySource, propertyOverrides);
                    if (!propertyOverrides.isEmpty()) {
                           environment.getPropertySources().addBefore(propertySource.getName(), new MapPropertySource("decoded"+propertySource.getName(), propertyOverrides));
                    }
            }
    

提交回复
热议问题