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

yaml文件加密

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

yaml文件加密

Springboot和jasypt

使用jasypt加密yaml文件中的敏感信息

1、所需依赖

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

2、使用在yaml文件中加入所需密钥

jasypt:
  encryptor:
    password: qwerty  #密钥(自定义)
    algorithm: PBEWithMD5AndDES #加密方式

3、密钥生成

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password="qwerty" algorithm=PBEWithMD5AndDES

input:密码(自定义)
password:自定义的密钥
algorithm:加密方式

4、把步骤3的命令在jasypt依赖下执行(cmd)

5、加密方法(以连接数据库的账号密码为例)

    @Autowired
    private StringEncryptor stringEncryptor;

    @Test
    public void encryptPwd(){

        //加密
        String username = stringEncryptor.encrypt("root");
        System.out.println("加密username:"+username);

        String decryptUserName = stringEncryptor.decrypt(username);
        System.out.println("解密username"+decryptUserName);

        String pwd = stringEncryptor.encrypt("123456");
        System.out.println("加密密码:"+pwd);

        String decryptPwd = stringEncryptor.decrypt(pwd);
        System.out.println("解密密码"+decryptPwd);

    }

6、再根据加密出来的账号密码替换掉yaml文件里的账号密码即可

   username: ENC(+9yooqRfPCwdB2a+qMKZUA==)
   password: ENC(vBoYnMFjRK8JUWTJo2XQJA==)

一定要加ENC()不然识别不了

也可以在springboot的启动类里加密钥(在启动类里加就不需要在配置文件中写了,只需要指定加密方式即可)

public class DemoApplication {

    public static void main(String[] args) {
        System.setProperty("jasypt.encryptor.password", "qwerty");
        SpringApplication.run(DemoApplication.class, args);
    }

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

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

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