About this task
If you are using a Java client and using OSGi configuration parameters, then follow these steps.
To use jasypt with spring components:
Procedure
-
Add the following Maven dependencies:
<dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.jasypt</artifactId> <version>1.9.2_1</version> </dependency> <dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.jasypt-spring31</artifactId> <version>1.9.2_1</version> </dependency>
-
Add the usual properties, but the encrypted property should be wrapped by the
ENC()
function.<osgix:cm-properties id="properties" persistent-id="my"> <prop key="mydb.password">ENC(fAdXf/ed2k+2uD1LhoOpvw==)</prop> </osgix:cm-properties>
-
Add the password and the algorithm configuration for encryptor.
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"> <property name="algorithm" value="PBEWITHSHA256AND128BITAES-CBC-BC"/> <property name="password" value="TESB_ENV_PASSWORD"/> </bean>
-
Create the encryptor and inject configuration to it.
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> <property name="config" ref="environmentVariablesConfiguration"/> </bean>
-
Add the property configurer and pass the properties to it. The
EncryptablePropertyPlaceholderConfigurer
will read the .properties files and make their values accessible as${var}
.<bean id="propertyConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderCon figurer"> <constructor-arg ref="configurationEncryptor"/> <property name="properties" ref="properties"/> </bean>
-
Now, you can use the properties as usual:
<bean id="serviceBean" class="org.company.example.MyServiceImpl"> <property name="prop" value="${mydb.password}"/> </bean>