I'm using Spring Boot 1.2.3
and I'd like to understand if it's possible to decrypt a property value before its injected into a bean annotated with @ConfigurationProperties
.
Suppose I have the following in an application.properties
file:
appprops.encryptedProperty=ENC(ENCRYPTEDVALUE)
and a sample application like so:
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;
}
}
}
In the past I've used a custom PropertySourcesPlaceholderConfigurer
to achieve this but it requires setting up a structure like the following:
@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;
}
}
While that in and of itself is not bad I'd like to see if I can leverage the niceties of @ConfigurationProperties
with encrypted properties.