通过Flyway进行Spring Boot-使用动态数据库密码

时间:2019-09-18 16:07:20

标签: spring-boot flyway

我想将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属性文件中

如何使用主要数据源配置它

2 个答案:

答案 0 :(得分:0)

在这种情况下,您可以使用环境变量来定义密码。 为此,您可以将属性设置为:

swat.datasource.password=${SWAT_PASSWORD}

,当您部署或运行​​应用程序时,只需在您的环境中设置SWAT_PASSWORD变量即可。

您还可以使用默认值,如果未设置变量,系统将使用默认值

swat.datasource.password=${SWAT_PASSWORD:default_password}

答案 1 :(得分:0)

您可以通过向Flyway添加自己的数据源和配置来覆盖Flyway Bean,在其中您可以传递自己的解密密码。现在,您将不得不在此Bean中手动配置Flyway,因为自动配置将不再起作用,以便使用其他Flyway属性。

@Configuration
public class FlywayConfiguration {

    @Value("${spring.flyway.url}")
    private String flywayUrl;

    @Value("${spring.flyway.user}")
    private String flywayUser;

    @Value("${spring.flyway.password}")
    private String flywayPassword;

    // can still use spring.flyway.enabled
    @ConditionalOnProperty(prefix = "spring.flyway", name = "enabled", matchIfMissing = true)
    @ConfigurationProperties(prefix = "spring.flyway")
    @Bean(initMethod = "migrate")
    public Flyway flyway() {

        DataSource dataSource = new DataSource();

        try {
            PoolProperties prop = new PoolProperties();
            prop.setUrl(flywayUrl);
            prop.setUsername(flywayUser);
            prop.setPassword(flywayPassword); // Add custom code to decrypt password
            dataSource = new DataSource(prop);
        } catch (Exception e) {
            log.error(e);
            log.error(e.getMessage());
        }

        // Configure Flyway with other properties if necessary
        return Flyway.configure().dataSource(dataSource).load();
    }
}

有关Flyway Java API的更多信息: