本文介绍了防止覆盖 application.properties 中的某些属性 - Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了安全起见,我想在我的属性文件中移动一些配置,但不希望它在运行时被覆盖.可以在spring boot中做吗?

I want to move some configuration in my property file, but don't want it to be overwritten at runtime, for security purpose. Is it possible to do it in spring boot?

谢谢,曼尼什

推荐答案

这样的事情应该可以工作,secure.properties 中的任何内容都将无法被覆盖,因为它将被添加到 env 的开头(从属性文件),无论什么已被覆盖.

Something like this should work, anything in secure.properties will not be able to be overridden as it will be added to the start of the env (from the properties file), regardless of what has been overridden.

@Configuration
public class SecurePropertiesConfig {

    @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);
        }
    }
}

这篇关于防止覆盖 application.properties 中的某些属性 - Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 01:12