How to decrypt properties used in @ConfigurationProperties beans?

I am using Spring Boot 1.2.3 , and I would like to understand if it is possible to decrypt the property value before entering it into the bean annotated using @ConfigurationProperties .

Suppose the following is in the application.properties file:

appprops.encryptedProperty=ENC(ENCRYPTEDVALUE)

and an example application as follows:

 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 used a custom PropertySourcesPlaceholderConfigurer to achieve this, but this requires creating a structure such as:

 @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 this in itself is not bad, I would like to see if I can use @ConfigurationProperties subtleties with encrypted properties.

+6
source share
3 answers

you can use org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer you can add the following Spring configuration in the Spring context XML file.

 <context:property-placeholder location="classpath:application.properties"/> <bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer"> <constructor-arg ref="configurationEncryptor" /> <property name="location" value="classpath:application.properties" /> </bean> <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> <property name="algorithm" value="PBEWithMD5AndDES" /> <property name="password" value="password" /> </bean> 
0
source

Just release the following file in your spring project and follow your own decryption method.

 @Component public class CmtEncryptedPropertyConfigurer extends PropertySourcesPlaceholderConfigurer { private ConfigurableEnvironment environment; @Override public void setEnvironment(Environment environment) { super.setEnvironment(environment); this.environment = (ConfigurableEnvironment) environment; } @Override protected void loadProperties(Properties props) throws IOException { this.localOverride = true; for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { String[] propertyNames = ((EnumerablePropertySource) propertySource).getPropertyNames(); for (String propertyName : propertyNames) { String propertyValue = propertySource.getProperty(propertyName).toString(); // put logic to see if decryption required for thsi name/value // decrypt here String decryptedValue = decrypt(propertyValue); // set value here props.setProperty(propertyName, decryptedValue); } } } }} 
0
source

The PropertyResourceConfigurer property has a convertPropertyValue method that you can override for this purpose.

https://docs.spring.io/autorepo/docs/spring/4.1.6.RELEASE/javadoc-api/org/springframework/beans/factory/config/PropertyResourceConfigurer.html#convertPropertyValue-java.lang.String-

 public class EncryptedPropertySourcedPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer { @Override protected String convertPropertyValue(String originalValue) { if(originalValue.startswith("ENC") { return decrypt(originalValue.subString(4, originalValue.length() - 1); } return originalValue; } } 
0
source

Source: https://habr.com/ru/post/986222/


All Articles