我正在使用Spring Boot 1.2.3
,我想了解是否可以在将属性值注入到注释为@ConfigurationProperties
的bean之前解密属性值。如何解密@ConfigurationProperties bean中使用的属性?
假设我有在application.properties
文件中的以下内容:
appprops.encryptedProperty=ENC(ENCRYPTEDVALUE)
和示例应用程序,像这样:
package aaa.bb.ccc.propertyresearch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import javax.annotation.PostConstruct;
@SpringBootApplication
@EnableConfigurationProperties(PropertyResearchApplication.ApplicationProperties.class)
public class PropertyResearchApplication {
public static void main(String[] args) {
SpringApplication.run(PropertyResearchApplication.class, args);
}
@ConfigurationProperties("appprops")
public static class ApplicationProperties {
private String encryptedProperty;
@PostConstruct
public void postConstruct() throws Exception {
System.out.println("ApplicationProperties --> appprops.encryptedProperty = " + encryptedProperty);
}
public String getEncryptedProperty() {
return encryptedProperty;
}
public void setEncryptedProperty(String encryptedProperty) {
this.encryptedProperty = encryptedProperty;
}
}
}
在过去,我已经使用了自定义PropertySourcesPlaceholderConfigurer
实现这一目标但它需要建立如下结构:
@Component
public class ApplicationProperties {
@Value("${appprops.enrcyptedProperty}")
private String encryptedProperty;
@PostConstruct
public void postConstruct() throws Exception {
System.out.println("ApplicationProperties --> appprops.encryptedProperty = " + encryptedProperty);
}
public String getEncryptedProperty() {
return encryptedProperty;
}
}
虽然这本身并不坏,我想看看我是否可以利用具有加密属性的@ConfigurationProperties
的细微差别。
你见过这样的:http://stackoverflow.com/questions/24451110/creating-a-custom-jasypt-propertysource-in-springboot – koe
OP发现HTTP的答案:/ /stackoverflow.com/a/24486190/4094797。请检查链接以找到解决方案。 – user2339071