springboot整合jasypt对yml配置文件密码加密

1.引入依赖

		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>2.1.0</version>
		</dependency>
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot</artifactId>
			<version>2.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.jasypt</groupId>
			<artifactId>jasypt</artifactId>
			<version>1.9.2</version>
		</dependency>

2.启动类增加@EnableEncryptableProperties

@EnableEncryptableProperties

3.生成加密密码

public class Test {
    public static void main(String[] args) {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        //加密的算法,默认
        config.setAlgorithm("PBEWithMD5AndDES");
        //加密的盐
        config.setPassword("password");
        standardPBEStringEncryptor.setConfig(config);
        //需要加密的密码
        String password = "root";
        String encrypt = standardPBEStringEncryptor.encrypt(password);
        System.out.println(encrypt);

        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("password");
        System.out.println(encryptor.decrypt(encrypt));
    }
}

4.配置文件密码加密

    password: ENC(Kn7cQm1DtLutH/27dTPi3D(Q/ILQbOKK)

加密的密码放在括号里面

5.配置秘钥

#jasypt加密的密匙
jasypt:
  encryptor:
    password: root

6.放在配置文件容易泄露,可以配置启动命令

java -jar -Djasypt.encryptor.password=root XXX-xxxx.jar
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值