spring boot - Creating a custom Jasypt PropertySource in Springboot -


i'm using spring boot create simple web application accesses database. i'm taking advantage of autoconfiguration functionality datasource setting spring.datasource.* properties in application.properties. works brilliantly , quick - great work guys @ spring!

my companys policy there should no clear text passwords. therefore need have sping.datasource.password encrypted. after bit of digging around decided create org.springframework.boot.env.propertysourceloader implementation creates jasypt org.jasypt.spring31.properties.encryptablepropertiespropertysource follows:

public class encryptedpropertysourceloader implements propertysourceloader {     private final standardpbestringencryptor encryptor = new standardpbestringencryptor();      public encryptedpropertysourceloader()     {         //todo: taken environment variable         this.encryptor.setpassword("password");      }      @override     public string[] getfileextensions()     {         return new string[]{"properties"};     }      @override     public propertysource<?> load(final string name, final resource resource, final string profile) throws ioexception     {         if (profile == null)         {             final properties props = propertiesloaderutils.loadproperties(resource);              if (!props.isempty())             {                 return new encryptablepropertiespropertysource(name, props, this.encryptor);             }         }          return null;     } } 

i packaged in it's own jar meta-inf/spring.factories file follows:

org.springframework.boot.env.propertysourceloader=com.mycompany.spring.boot.env.encryptedpropertysourceloader 

this works when run maven using mvn spring-boot:run. problem occurs when run standalone war using java -jar my-app.war. application still loads fails when try connect database password value still encrypted. adding logging reveals encryptedpropertysourceloader never loaded.

to me sounds classpath issue. when run under maven jar loading order strict once under embebed tomcat there nothing custom jar should loaded before spring boot.

i've tried adding following pom.xml ensure classpth preserved doesn't seem have had effect.

<build>     <pluginmanagement>         <plugins>             <plugin>                 <groupid>org.apache.maven.plugins</groupid>                 <artifactid>maven-war-plugin</artifactid>                 <configuration>                     <failonmissingwebxml>false</failonmissingwebxml>                     <archive>                         <manifest>                             <mainclass>${start-class}</mainclass>                             <addclasspath>true</addclasspath>                         </manifest>                     </archive>                 </configuration>             </plugin>         </plugins>     </pluginmanagement>     <plugins>         <plugin>             <groupid>org.springframework.boot</groupid>             <artifactid>spring-boot-maven-plugin</artifactid>         </plugin>     </plugins> </build> 

does have ideas? in advance.

update:

a step forward: i've managed fix having encryptedpropertysourceloader class implement org.springframework.core.priorityordered interface , returning highest_precedence getorder(). has fixed issue of propertysourceloader not being used. it's throwing following error when tries decrypt properties:

org.jasypt.exceptions.encryptioninitializationexception: java.security.nosuchalgorithmexception: pbewithmd5anddes secretkeyfactory not available     @ org.jasypt.encryption.pbe.standardpbebyteencryptor.initialize(standardpbebyteencryptor.java:716)     @ org.jasypt.encryption.pbe.standardpbestringencryptor.initialize(standardpbestringencryptor.java:553)     @ org.jasypt.encryption.pbe.standardpbestringencryptor.decrypt(standardpbestringencryptor.java:705)     @ org.jasypt.properties.propertyvalueencryptionutils.decrypt(propertyvalueencryptionutils.java:72)     @ org.jasypt.properties.encryptableproperties.decode(encryptableproperties.java:230)     @ org.jasypt.properties.encryptableproperties.get(encryptableproperties.java:209)     @ org.springframework.core.env.mappropertysource.getproperty(mappropertysource.java:36)     @ org.springframework.boot.env.enumerablecompositepropertysource.getproperty(enumerablecompositepropertysource.java:49)     @ org.springframework.boot.context.config.configfileapplicationlistener$configurationpropertysources.getproperty(configfileapplicationlistener.java:490) 

again doesn't happen when running mvn spring-boot:run happen when running executable war file. both scenarios use same jvm (jdk1.6.0_35). results on google/stackoverflow suggest issue java security policy work when run maven think can discount that. possibly packaging issue...

you give try: jasypt-spring-boot wraps propertysource present in environment encryptable version. 2 things gotta once import library (adding dependency if use maven) annotate @configuration class @enableencryptableproperties, , configure encryption algorithm , password through properties.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -