使用Jasypt3.0.3版本对SpringBoot配置文件加密

时间 2023-11-01
使用Jasypt3.0.3版本对SpringBoot配置文件加密

引入依赖

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

使用密钥生成密文

	@Test
    public void testEncrypt(){
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        // 加密密钥
        textEncryptor.setPassword("123456");

        String username = textEncryptor.encrypt("your-username");
        String password = textEncryptor.encrypt("your-password");
        System.out.println("username:" + username);
        System.out.println("password:" + password);
    }

输出结果

username:i8QgEN4uOy2E1rHzrpSTYA==
password:6eaMh/RX5oXUVca9ignvtg==

配置yml

# yml文件敏感信息加解密
jasypt:
    encryptor:
        property:
            prefix: "ENC("
            suffix: ")"
        password: 123456
        algorithm: PBEWithMD5AndDES
        string-output-type: String
        iv-generator-classname: org.jasypt.iv.NoIvGenerator

spring:
	datasource:
		username=ENC(i8QgEN4uOy2E1rHzrpSTYA==)
		password=ENC(6eaMh/RX5oXUVca9ignvtg==)

验证是否自动解密

能在本地启动程序就算成功。

部署时配置密钥值,为了防止密钥泄露,反解出密码.可以在项目部署的时候使用命令传入密钥值

java -jar -Djasypt.encryptor.password=123456 xxx.jar
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值