springboot 配置通过jasypt进行加密

第一步:引入依赖

        <!-- jasypt加密 -->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

第二步:启动类注解

@SpringBootApplication
@EnableEncryptableProperties
public class DemoApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

第三步:yml配置

# Jasypt加密配置
jasypt:
  encryptor:
    #生产环境非此密钥
    password: 1234567890
    #指定解密算法
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

第四步:生成密文

    @Test
    void test() throws InterruptedException {
        System.out.println(encrypt("root", "1234567890"));
    }

    public static String encrypt(String password, String factor) {
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //秘钥
        config.setPassword(factor);
        //应用配置
        encryptor.setConfig(config);
        //加密
        return encryptor.encrypt(password);
    }

第五步:将第四步生成的密文通过ENC()包裹

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: ENC(4agwjbpTuk8qGGKaco0Rsg==)
    url: jdbc:mysql://127.0.0.1:3306/dms?useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: ENC(Pfydgw/yIGmEvj2rUBlRDQ==)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值