Quick Jasypt Spring 3 Tutorial

  • Post author:
  • Post category:Java

This is a quick tutorial on setting up Jasypt with Spring 3. You will need to be familiar with Maven and m2e to follow the steps below.

1. Create a new Maven Project using the Eclipse New Wizard. When prompted for the archetype, pick the spring-mvc-jpa-archetype. This will give us a running Spring MVC web application.

2. Enter com.teamextension for the groupId. Enter quickjasypt for the artifactId. Enter com.teamextension.quickjasypt for the package. Hit Finish.

3. Right click on the quickjasypt project and go to Maven -> Add Dependency. Enter the groupId org.jasypt and the artifactId jasypt. This adds the Jasypt library to our Spring MVC application.

4. Edit src/main/webapp/WEB-INF/spring/db.xml. Remove the existing placeHolderConfig bean and add the beans:

  1. EnvironmentStringPBEConfig
  2. StandardPBEStringEncryptor
  3. EncryptablePropertyPlaceholderConfigurer
<bean id="encryptorConfig" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
  	<property name="algorithm" value="PBEWithMD5AndDES" />
  	<property name="passwordEnvName" value="PASSWORD_ENV_VARIABLE" />
</bean>

<bean id="encryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
	<property name="config" ref="encryptorConfig" />
</bean>

<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
	<constructor-arg ref="encryptor" />
	<property name="locations">
   		<list>
   			<value>/WEB-INF/classes/db.properties</value>
   		</list>
	</property>
</bean>

5. Open src/main/resources/db.properties and you will see the password is in plain text. Now let’s use Jasypt to encrypt our passwords.

6. Download and unzip the Jasypt distribution, then open a command line at at the bin directory. Encrypt the password by running

encrypt input="database password" password="master password" algorithm="PBEWITHMD5ANDDES"

7. Copy the output of the encrypt command and update the password in db.properties. Enclose the encrypted password with ENC().

db.username=sa
db.password=ENC(8XPVPCbKt51RZYG1gPydm1x2JE15wX3s)
db.driver=org.hsqldb:mem:app-db
db.dialect=org.hibernate.dialect.HSQLDialect

8. In the runtime/deployment environment, make sure an environment variable PASSWORD_ENV_VARIABLE=master password is set. This is the master password entered in the encrypt command. Note that the PASSWORD_ENV_VARIABLE is specified in the encryptorConfig bean in db.xml.

For more information, please visit the Jasypt with Spring Framework page.

This Post Has 7 Comments

  1. Murthy

    java.lang.NullPointerException
    at org.jasypt.encryption.pbe.config.SimplePBEConfig.getPasswordCharArray(SimplePBEConfig.java:434)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.resolveConfigurationPassword(StandardPBEByteEncryptor.java:740)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:590)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:111)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:121)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperties(PropertyResourceConfigurer.java:104)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:81)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
    at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:140)
    at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:84)
    at com.technicolor.staging.utilities.PreStagingUtil.getDbcon(PreStagingUtil.java:278)
    at com.technicolor.staging.main.PreStagingMain.main(PreStagingMain.java:42)

  2. Amit

    I have configured as above steps, how do I run the war file

  3. Amit

    Thanks it worked.

  4. Mohit

    Hi,

    I have configured the same and I am getting the below mentioned error:
    java.lang.NullPointerException
    at org.jasypt.encryption.pbe.config.SimplePBEConfig.getPasswordCharArray(SimplePBEConfig.java:434)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.resolveConfigurationPassword(StandardPBEByteEncryptor.java:740)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:590)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer.convertPropertyValue(EncryptablePropertyPlaceholderConfigurer.java:111)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperty(PropertyResourceConfigurer.java:121)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperties(PropertyResourceConfigurer.java:104)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:81)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)

  5. Mohit

    I appreciate any help

  6. husain

    same errror i am facing

  7. Sudarshani

    Did you get resolve it?

Comments are closed.