java - 防止覆盖 application.properties 中的某些属性 - Spring Boot

标签 java spring spring-boot properties

出于安全目的,我想移动属性文件中的一些配置,但不希望在运行时覆盖它。可以在 Spring Boot 中实现吗?

谢谢, 曼尼什

最佳答案

这样的东西应该可以工作,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);
        }
    }
}

关于java - 防止覆盖 application.properties 中的某些属性 - Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47039289/

相关文章:

java - java 的应用程序引擎问题,在生产中存在前端实例并阻止请求

java - Spring JSON View

java - 如果 yml 文件不存在,则在 mysql 中创建数据库时出错

java - Spring boot - 不支持请求方法 'POST'。尝试了一切

java - Elasticsearch 因 NoNodeAvailableException 而失败

java - 如何摆脱 PermGen 空间?

java - 如何在 Spring GET servlet 中保留空值?

java - 当一行更改为较大字体时如何重绘 JTextPane

spring - 在 Feign 客户端 + Spring Cloud (Brixton RC2) 中使用带有动态值的 @Headers

Spring Boot - 不再在配置上调用 destroy()