Skip to main content Skip to complementary content

OSGI Configuration Parameters for Spring Components

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

  1. 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>
  2. 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>
  3. 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> 
  4. Create the encryptor and inject configuration to it.
    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
       <property name="config" ref="environmentVariablesConfiguration"/>
    </bean>
  5. 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>
  6. Now, you can use the properties as usual:
    <bean id="serviceBean" class="org.company.example.MyServiceImpl">
        <property name="prop" value="${mydb.password}"/>
    </bean>

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!