jasypt-spring-boot-starter实现加解密和数据返显

jasypt-spring-boot-starter实现加解密和数据返显

  1. pom.xml
<dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
  1. 密文的生成
public class EncryptConfigUtil {
          
   
    public static void main(String[] args) {
          
   

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword("123456");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("123456");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }
}
  1. 修改配置文件
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test01?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    username: ENC(tK8xqlIRBn6Y27AYzUihYg==)
    password: ENC(ULizg8E8GjJfTxzv78vwVg==)
  1. 盐值的配置 application.yml 文件(不推荐) jasypt.encryptor.password=123456 推荐在JVM启动参数中设置 在idea中配置如下: -Djasypt.encryptor.password=123456
经验分享 程序员 微信小程序 职场和发展