Spring Boot Redis密码加密
引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.2</version>
</dependency>
配置Bean
@Configuration
public class EncryptorConfig {
@Bean("jasyptStringEncryptor")
public StringEncryptor jasyptStringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7");
// 注释部分为配置默认
config.setAlgorithm("PBEWithMD5AndDES");
config.setPoolSize("1");
encryptor.setConfig(config);
return encryptor;
}
}
启动类添加注解@EnableEncryptableProperties
@ImportResource({"classpath:/META-INF/start-config.xml"})
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
@EnableCaching
@EnableAsync
@EnableDubbo
@EnableUcModuleRedisConfiguration
@EnableConfigurationProperties({
RedisExtendProperties.class,
JwtProperties.class,
SystemConfigProperties.class
})
@EnableEncryptableProperties
public class BootApplication {
private static Logger logger = LoggerFactory.getLogger(BootApplication.class);
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(BootApplication.class);
springApplication.setWebApplicationType(WebApplicationType.SERVLET);
springApplication.run(args);
logger.info("SpringBootApplication Boot Success ");
}
}
生成密文
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="asd123" password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7 algorithm=PBEWithMD5AndDES
属性文件添加密文
spring.redis.password=ENC(2RP1Vdsa+2wdSOgu2biAJkTCU9fnkUGD)
启动测试即可。