使用Jasypt对SpringBoot配置文件加密

引入jasypt


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

1 在application.properties文件填写自己的加密钥匙

jasypt.encryptor.password=81*(!@92*
demo.username=ENC(jQl99b3T0G7WNYsj4x/98w==)
demo.password=ENC(H872ZicbMaJBd2PiIITDWw==)

2 在com.eujian.jasypt.Main里面填写配置文件的加密钥匙串,然后运行main函数

package com.eujian.jasypt;

import org.jasypt.util.text.BasicTextEncryptor;

public class Main {
    public static void main(String[] args) {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("81*(!@92*");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("test");
        System.out.println("demo.username:"+username);
        System.out.println("demo.password:"+password);
    }
}

3 运行单元测试,测试解密com.eujian.jasypt.JasyptApplicationTests

package com.eujian.jasypt;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class JasyptApplicationTests {

    @Value("${demo.username}")
    private String username;

    @Value("${demo.password}")
    private String password;
    @Test
    void contextLoads() {
        System.out.println("username="+username);
        System.out.println("password="+password);
    }

}

运行结果如下

username=root
password=test

官方地址 :
https://github.com/hd-eujian/springboot-Jasypt.git
https://gitee.com/guoeryyj/springboot-Jasypt.git

你可能感兴趣的:(使用Jasypt对SpringBoot配置文件加密)