【问题标题】:Prevent overriding some property in application.properties - Spring Boot防止覆盖 application.properties 中的某些属性 - Spring Boot
【发布时间】:2018-04-12 20:26:22
【问题描述】:

出于安全考虑,我想在我的属性文件中移动一些配置,但不希望它在运行时被覆盖。可以在spring boot中做到吗?

谢谢, 马尼什

【问题讨论】:

    标签: java spring spring-boot properties


    【解决方案1】:

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

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

    【讨论】:

    • 为什么在 setter 中同时拥有字段和参数?该字段可能可以删除?
    • @KeesvanDieren 是的,很好发现
    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2020-05-03
    • 1970-01-01
    • 2020-03-04
    • 2020-07-15
    相关资源
    最近更新 更多