【发布时间】:2020-01-20 11:46:23
【问题描述】:
我想将flyway集成到我的spring boot项目中,但是我无法在属性文件中将密码写入数据库。
对于我的普通数据源,我使用以下代码
@Configuration
@Slf4j
public class SwatDataBaseConfig {
@Value("${swat.decrypt.location}")
private String fileLocation;
@Value("${swat.datasource.url}")
private String dbURL;
@Value("${swat.datasource.driver-class-name}")
private String driverName;
@Value("${swat.datasource.username}")
private String userName;
@Value("${swat.datasource.password}")
private String hashedPassword;
@Bean
public DataSource primaryDataSource() {
// String password = encryptor.decrypt(hashedPassword);
// log.debug("password is: " + password);
String password = null;;
Process process = null;
try {
process = new ProcessBuilder(fileLocation, hashedPassword).start();
} catch (IOException e) {
log.error("Could not read the file", e);
return null;
}
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
try {
while ((line = br.readLine()) != null) {
password = line;
try {
password = line.split(" ")[1].trim();
}catch(Exception ex) {
log.error("Error while sanitating the password", ex);
}
}
} catch (IOException e) {
log.error("Could not process the file output", e);
}
PoolProperties poolProperties = new PoolProperties();
poolProperties.setUrl(dbURL);
poolProperties.setUsername(userName);
poolProperties.setPassword(password);
poolProperties.setDriverClassName(driverName);
poolProperties.setTestOnBorrow(true);
poolProperties.setValidationQuery("SELECT 1");
poolProperties.setValidationInterval(0);
DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(poolProperties);
process.destroy();
return ds;
// return DataSourceBuilder.create().url(dbURL).driverClassName(driverName).username(userName).password(password).build();
}
这会从属性中获取我的加密密码,对其进行解密,然后将其传递给数据库, 在我见过的所有 flyway 示例中,密码都以纯文本形式写入 flyway 属性文件中
如何按照我使用主数据源的方式配置它
【问题讨论】:
标签: spring-boot flyway