Spring Boot Redis密码加密

2 篇文章 0 订阅
1 篇文章 0 订阅

引入依赖

<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) 

启动测试即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值