一针见血教程:SpringBoot yml配置采用jasypt加密(encrypt)解密(decrypt)方式

  • Maven pom配置
		<!--加密问题-->
        
            com.github.ulisesbocchio
            jasypt-spring-boot-starter
            1.15
        
  • yml配置
jasypt:
    encryptor:
        password: xxx
  • Java代码
package com.xxx.xxx.util;

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

public class JasyptPasswordTest {


    @Test
    public void encrypt() {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("xxx");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("123456");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }

    @Test
    public void decrypt() {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("xxx");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.decrypt("9fwLpdF84L9lYEAMLkKA/Q==");
        String password = textEncryptor.decrypt("REwz67bF8fP2Fhd9v+ApXQ==");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }
}

你可能感兴趣的:(技术)