【发布时间】:2018-04-12 20:26:22
【问题描述】:
出于安全考虑,我想在我的属性文件中移动一些配置,但不希望它在运行时被覆盖。可以在spring boot中做到吗?
谢谢, 马尼什
【问题讨论】:
标签: java spring spring-boot properties
出于安全考虑,我想在我的属性文件中移动一些配置,但不希望它在运行时被覆盖。可以在spring boot中做到吗?
谢谢, 马尼什
【问题讨论】:
标签: java spring spring-boot properties
这样的事情应该可以工作,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);
}
}
}
【讨论】: