java - How to decrypt properties used in @ConfigurationProperties beans? -


i'm using spring boot 1.2.3 , i'd understand if it's possible decrypt property value before injected bean annotated @configurationproperties.

suppose have following in application.properties file:

appprops.encryptedproperty=enc(encryptedvalue)

and sample application 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 past i've used custom propertysourcesplaceholderconfigurer achieve requires setting structure 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 in , of not bad i'd see if can leverage niceties of @configurationproperties encrypted properties.

you can use org.jasypt.spring.properties.encryptablepropertyplaceholderconfigurer can add following spring configuration in 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> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -