An even more flexible approach is something like the following:
1. Create a general interface for cryption, which can be either
direction
Code:
public interface Cryptor {
String crypt(String value);
}
2. Create a resolver for a value, which takes any Cryptor implementation
Code:
public class CryptorResolver implements FactoryBean {
private String value;
private Cryptor cryptor;
public void setValue(String value) {
this.value = value;
}
public void setCryptor(Cryptor cryptor) {
this.cryptor = cryptor;
}
public Object getObject () throws Exception {
return cryptor.crypt(value);
}
public Class getObjectType () {
return String.class;
}
public boolean isSingleton () {
return false;
}
}
3. Create encryptor and decryptor implementations
Code:
public class EncryptorImpl implements Cryptor { ... }
public class DecryptorImpl implements Cryptor { ... }
4. In your beans.xml, define system wide encryptor and decryptor
Code:
<bean id="encryptor" class="EncryptorImpl" isSingleton="true"/>
<bean id="decryptor" class="DecryptorImpl" isSingleton="true" />
5. When you want to decrypt, create an instance of the resolver
Code:
<bean id="passwordDecrypter" class="EncryptionResolver">
<property name="cryptor"><ref bean="decryptor" /></property>
<property name="value"><value>${epwd}</value></property>
</bean>
This approach is much more generic, and allows your application internally to encrypt decrypt items. This is helpful if you store any passwords in the database, such as for external datasources which
are not part of the static configuration, and so forth.