【Spring Cloud】配置中心密码加密

139 阅读1分钟
  • 配置中心 spring config的数据库配置
spring:
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/gajcz
    type: com.alibaba.druid.pool.DruidDataSource
    username: ENC(Y0nVVJF35lDNZElo88YKQ/+IK8+R+92SSYLd3P1rP5sC1KdJ0Bs0YpfTGUmQL0SH)
    password: ENC(rCaalcRucllKT7Oe9PFrngVgLe49AyUaEHzz9zdrvSkCXJwkHWvEhENGimQi0I+t)
    driver-class-name: org.postgresql.Driver
    filters: stat,wall,log4j
  • 服务中添加配置类型
@Configuration
@EnableEncryptableProperties
public class JasyptConfig {

    @Bean("stringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("gass");
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

}
  • 启动类添加密码
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ViidCollectApplication {

    public static void main(String[] args) {
        System.setProperty("jasypt.encryptor.password", "root123");
        SpringApplication.run(ViidCollectApplication.class, args);
    }
}