防止重写application.properties-spring boot中的某些属性

a8jjtwal  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(325)

我想在我的属性文件中移动一些配置,但为了安全起见,不想在运行时覆盖它。可以在Spring Boot里做吗?
谢谢,曼尼什

0h4hbjxa

0h4hbjxa1#

类似这样的操作应该可以工作,secure.properties中的任何内容都将无法被重写,因为它将被添加到env的开头(从properties文件),而不管重写了什么。

@Configuration
public class SecurePropertiesConfig {

    @Autowired
    private ConfigurableEnvironment env;

    @Autowired
    public void setConfigurableEnvironment(ConfigurableEnvironment env) {
        try {
            final Resource resource = new ClassPathResource("secure.properties");
            env.getPropertySources().addFirst(new PropertiesPropertySource(resource.getFilename(), PropertiesLoaderUtils.loadProperties(resource)));
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }
}

相关问题