栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot项目使用jasypt对mysql数据库配置进行账号密码加密

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot项目使用jasypt对mysql数据库配置进行账号密码加密

1.引入依赖

		
			com.github.ulisesbocchio
			jasypt-spring-boot-starter
			1.14
		
		
			com.github.ulisesbocchio
			jasypt-spring-boot
			1.14
		
		
2.yml配置添加你自己的盐值
#jasypt加密的盐值
jasypt:
  encryptor:
    password: mykey
3.写测试类生成密码
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.Test;


public class JasyptTest {
//    加密
    @Test
    public void testEncrypt() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法,这个算法是默认的
        config.setPassword("rfid");                        // 加密的密钥
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "root";
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println("加密后的密码:"+encryptedText);
    }
// 解密
//    @Test
//    public void testDe() throws Exception {
//        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
//        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
//
//        config.setAlgorithm("PBEWithMD5AndDES");
//        config.setPassword("D30CCEEC367C55F0E4C7B3BE8207C5DD");
//        standardPBEStringEncryptor.setConfig(config);
//        String encryptedText = "a2Dg/nGebGU7dlTTaI93x4KVA0UMd0IvdMIs1OYHZCU=";
//        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
//        System.out.println(plainText);
//    }

}

 示例 :LRXUUqO9Az60LdzTzfEPow==

4.放到你的数据库配置生成以后的密钥,注意要用 ENC() 包起来   如下 
ENC(LjaWI1eGcY02T1LLeZyLPg==)
5.放到数据库的指定配置位置就行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/945356.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号