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

1.引入依赖

		
			com.github.ulisesbocchio
			jasypt-spring-boot-starter
			2.1.0
		
		
			com.github.ulisesbocchio
			jasypt-spring-boot
			2.1.0
		

		
			org.jasypt
			jasypt
			1.9.2
		

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

你可能感兴趣的:(spring,boot,后端,java)