I have an application.properties file in a Spring boot application which has the db Url, username, password and few other properties. The password is in an encrypted form. I have the decryption logic in a java function. How can I decrypt the password before Spring boot creates a datasource.
CodePudding user response:
You can use @Value annotation
@Value("${mypassword}")
private String encryptedPass;
then you can call the decryption function with the encryptedPass value
decryptionFun(encryptedPass);
This should return a string and you can use it an decrypted password .
For more information read the external config docs .
CodePudding user response:
You can do so by creating Datasource programmatically as below:-
@Configuration
public class DataSourceConfig {
@Autowired
private Environment environment;
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName(environment.getProperty("spring.datasource.driver-class-name"));
dataSourceBuilder.url(environment.getProperty("spring.datasource.url"));
dataSourceBuilder.username("test");//environment.getProperty("spring.datasource.user")
dataSourceBuilder.password(decryptPassword("encyptedPasswordFromPropertiesFile"));//environment.getProperty("spring.datasource.password")
return dataSourceBuilder.build();
}
}
Use your custom method to decrypt the password.
private String decryptPassword(String encycptedPassword)){
// Decryption Logic
}