在Springboot中通过jasypt 进行加密解密

通过UT创建工具类,并认识jasypt

import org.jasypt.util.text.BasicTextEncryptor;
import org.junit.Test;

public class UtilTests {
@Test
public void jasyptTest() {
    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    // application.properties, jasypt.encryptor.password‘’
   //该abc表示 jasypt.encryptor.password解密的密钥
    encryptor.setPassword("abc");
    // encrypt root 加密
    System.out.println(encryptor.encrypt("root"));
    System.out.println(encryptor.encrypt("root"));
    System.out.println(encryptor.encrypt("root"));

    // decrypt, the result is root,解密
    System.out.println(encryptor.decrypt("UP/yojB7ie3apnh3mLTU7w=="));
    System.out.println(encryptor.decrypt("ik9FE3GiYLiHwchiyHg9QQ=="));
    System.out.println(encryptor.decrypt("9Obo/jq9EqmTE0QZaJFYrw=="));
}

}

可以看出, 每次生成的密码是不一样的, 但是通过密钥,可以解密成一样的明文.

2.2 在SpringBoot中配置jasypt
2.2.1 配置密钥

        jasypt.encryptor.password:abc

2.2.2 使用

     spring.datasource.url: jdbc:mysql://127.0.0.1:3306/tmp?useSSL=false&useUnicode=true&characterEncoding=utf-8

     spring.datasource.username: ENC(ik9FE3GiYLiHwchiyHg9QQ==)

     spring.datasource.password: ENC(ik9FE3GiYLiHwchiyHg9QQ==)

     spring.datasource.driver-class-name: com.mysql.jdbc.Driver

2.2.3 启动时配置密钥

     java -jar -Djasypt.encryptor.password=abc xxx.jar

你可能感兴趣的:(springboot)