I created a micronaut application that has access to multiple datasources through jdbctemplate. I configured the jdbctemplates like so:
@Factory
@Requires(beans = DatasourceFactory.class)
public class JdbcTemplateFactory {
@Context
@EachBean(DataSource.class)
JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
This uses io.micronaut.configuration.jdbc.tomcat.DatasourceFactory which consumes my configuration yml:
datasources:
datasource111111:
url: url
username: username
password: password
driverClassName: org.h2.Driver
datasource222222:
url: url
username: username
password: password
driverClassName: org.h2.Driver
The issue is I want to somehow decrypt the password coming from configuration. My first attempt is to "replace" the DatasourceConfiguration bean that the factory is using but no luck, it gives me an error (io.micronaut.context.exceptions.DependencyInjectionException) that doesn't make sense.
Here's my replace attempt:
@Replaces(DatasourceConfiguration.class)
@EachProperty(value = BasicJdbcConfiguration.PREFIX, primary = "default")
public class EncryptedDatasourceConfiguration extends DatasourceConfiguration {
public EncryptedDatasourceConfiguration(String name) {
super(name);
}
@Override
public String getPassword() {
return "encrypted password";
}
}
Any idea what I'm doing wrong?? Thanks!
Answer: Had to listen to bean creation as suggested
@Singleton
public class DatasourceInitiliazer implements BeanCreatedEventListener<DatasourceConfiguration> {
@Override
public DatasourceConfiguration onCreated(BeanCreatedEvent<DatasourceConfiguration> event) {
final DatasourceConfiguration datasource = event.getBean();
datasource.setPassword("encryptedPassword");
return datasource;
}
}
You're most likely better off creating a BeanCreatedEventListener that reads the password, decrypts it, and sets it back on the configuration
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With