1

I am using spring mongo template for Mongo db server, i need to encrypt the password in the property file and decrypt in the Mongo template

I am using UserCredentials class to pass user name & password.

Can any one help how to overwrite this class to decrypt the encrypted password and pass into Mongo template

<bean id="simpleJdbcTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo"/>
<constructor-arg name="databaseName" value="mooadmin"/>
<constructor-arg name="userCredentials" ref="mongoCredentials"/>
</bean>

<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="${mongo.server}"/>
<property name="port" value="${mongo.port}"/>
</bean>

<bean id="mongoCredentials" 
  class="org.springframework.data.authentication.UserCredentials">
     <constructor-arg name="username" value="${mongo.username}" />
     <constructor-arg name="password" value="${mongo.password}" />
</bean>
user1032521
  • 11
  • 2
  • 6
  • Please see http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption – gerrytan Aug 22 '13 at 04:12
  • help needed to overwrite the class or way to decrypted password to Mongotemplate , not asking help for encrypt/decrypt mechanism – user1032521 Aug 22 '13 at 04:41

1 Answers1

3

See here for a encryption / decryption mechanism: Java 256-bit AES Password-Based Encryption

Assuming you've figured out how to decrypt string based on above link, you can simply override the password getters of UserCredentials class

package com.mycompany;

// imports..

public class UserCredentials extends org.springframework.data.authentication.UserCredentials {

  private String decrypt(String encryptedStr) {
    // your decryption code goes here...
  }

  @Override
  public String getPassword() {
     return decrypt(super.getPassword());
  }
}

Place the encrypted password on your properties file and setup the UserCredentials subclass in your spring xml config file:

<bean id="mongoCredentials" 
  class="com.mycompany.UserCredentials">
     <constructor-arg name="username" value="${mongo.username}" />
     <constructor-arg name="password" value="${mongo.encryptedPassword}" />
</bean>
Community
  • 1
  • 1
gerrytan
  • 40,313
  • 9
  • 84
  • 99