Question

How to convert below 2 xml configuration into java Config in Spring 4

1) jasypt

<encryption:encryptor-config id="eConf" password-env-name="APP_ENCRYPTION_PASSWORD" algorithm="PBEWithMD5AndDES" />

<encryption:string-encryptor id="stringEnc" config-bean="eConf" />

first part (encryption:encryptor-config) of encryption could be converted like this:

@Bean
public EnvironmentStringPBEConfig environmentVariablesConfiguration() {

    EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
    config.setAlgorithm("PBEWithMD5AndDES");
    config.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");
}

but, how to convert encryption:string-encryptor part.

2) profile

    <beans profile="dev">
        <util:properties id="myProps" location="classpath:dev.properties" />
    </beans>

    <beans profile="prod">
        <util:properties id="myProps" location="classpath:prod.properties" />
    </beans>

@PropertySource("classpath:prod.properties") is used for util:properties, but how to mention profile in the PropertySource annotation?

Était-ce utile?

La solution

Judging by the source code of jasypt library (EncryptionNamespaceHandler and EncryptorFactoryBean) and the API for PooledPBEStringEncryptor I'm assuming you can start experimenting with something like this:

@Bean
public EnvironmentStringPBEConfig environmentVariablesConfiguration() {
   EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
   config.setAlgorithm("PBEWithMD5AndDES");
   config.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");
   return config;
}
@Bean
public PooledPBEStringEncryptor stringEncryptor() {
   PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
   encryptor.setConfig(environmentVariablesConfiguration());
   return encryptor;
}

One note, though: I don't know anything about Jasypt, but I see their package names and documentation and everything is around spring 2, 3 and 3.1. Nothing about Spring 4. So, I don't say it won't work, but it is something to keep in mind in case things that look like they should work, but they don't.

As for @PropertySource I think you need something around these lines:

@Configuration
@Profile(value="prod")
@PropertySource("classpath:prod.properties")
public class ProdPlaceholderConfig {
...
}

@Configuration
@Profile(value="dev")
@PropertySource("classpath:dev.properties")
public class DevPlaceholderConfig {
...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top