• SpringBoot 之 Jasypt 实现yml配置文件加密


    大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。
    本篇主题是:SpringBoot 之 Jasypt 实现yml配置文件加密

    目录

    一、Jasypt3.0及以上版本(注意看版本)

    二、Jasypt3.0及以下版本

    在我们的项目中各种application-x.yml 配置文件经常会存储一些password的值,如果是明文存储其实是一种安全隐患,很多公司项目交付时是不能过的,我们可以使用SpringBoot的Jasypt 的方式进行加密。官网教学可以参考--https://github.com/ulisesbocchio/jasypt-spring-boot

    下面让我们开始,案例,加密数据库用户和密码

    一、Jasypt3.0及以上版本(注意看版本)

    1、引入Jasypt依赖 3.0.4,当前日期最新版本

    1. com.github.ulisesbocchio
    2. jasypt-spring-boot-starter
    3. 3.0.4

    2、假设yml中数据库配置如下,需要加密的是shenyun/shenyun20220907

    1. spring:
    2. datasource:
    3. # 二、非嵌入式数据库配置--MySQL
    4. driverClassName: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true
    6. username: shenyun
    7. password: shenyun20220907

    3、此时我们需要对它用Jasypt进行加密,重要,还有个秘钥-盐后面再提,假设盐值是"salt20220907",加密代码如下

    1. public static void main(String[] args) {
    2. PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    3. SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    4. // 加密方式
    5. config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
    6. // 盐值
    7. config.setPassword("salt20220907");
    8. config.setKeyObtentionIterations("1000");
    9. config.setPoolSize("1");
    10. config.setProviderName("SunJCE");
    11. config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    12. config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
    13. config.setStringOutputType("base64");
    14. encryptor.setConfig(config);
    15. // 加密明文
    16. String encryptValue1 = encryptor.encrypt("shenyun");
    17. String encryptValue2 = encryptor.encrypt("shenyun20220907");
    18. // 解密密文
    19. // String decryptValue = encryptor.decrypt("MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ");
    20. System.out.println(encryptValue1);
    21. System.out.println(encryptValue2);
    22. // System.out.println(decryptValue);
    23. }

    运行后此时我们得到了

    shenyun的密文为hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3

    shenyun20220907的密文为MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ

    4、修改yml文件为,用ENC(...),Jasypt就会识别

    1. spring:
    2. datasource:
    3. # 二、非嵌入式数据库配置--MySQL
    4. driverClassName: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true&serverTimezone=GMT%2B8
    6. username: ENC(hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
    7. password: ENC(MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ)
    8. jpa:
    9. hibernate:
    10. ddl-auto: update
    11. generate-ddl: true
    12. show-sql: true

    5、加上盐解密,默认是ENC(..)它就会解密,也可以在容器中用${value}取得这个密文。这个盐可以在启动JVM时带入,或者保存在云部署-credential上(盐值应该放在系统属性、命令行或是环境变量来使用),此时yml配置为

    1. spring:
    2. datasource:
    3. # 二、非嵌入式数据库配置--MySQL
    4. driverClassName: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true&serverTimezone=GMT%2B8
    6. username: ENC(hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
    7. password: ENC(MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ)
    8. jpa:
    9. hibernate:
    10. ddl-auto: update
    11. generate-ddl: true
    12. show-sql: true
    13. jasypt:
    14. encryptor:
    15. password: salt20220907
    16. # 通过jvm参数设置,直接-Djasypt.encryptor.password=salt20220907
    17. # 默认解密识别是是ENC(..),如果需要定制不同,可以进行下面配置,如下ENC@[..]
    18. # property:
    19. # prefix: "ENC@["
    20. # suffix: "]"

    6、启动Java程序,发现正常连接数据库则解密成功。

    7、因为盐放在了yml上面,也是不安全的,我们可以通过启动参数去配置它,或者配置到云中的credentitl上。下面我将yml的jasypt删掉,即

    1. spring:
    2. datasource:
    3. # 二、非嵌入式数据库配置--MySQL
    4. driverClassName: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/jpa?useUnicode=true&useSSL=true&serverTimezone=GMT%2B8
    6. username: ENC(hEc7NPysxK/gIxN1r6iogy/sadZhFkBLLm+gr55CQKC0nBgCSJXEyU9PmsGi1Li3)
    7. password: ENC(MaF3AwiV/aXCCDMEx+nOgAtXreq62JMgRM+2M9NIpUe0vko3fahZ+IxnvocfaGnJ)
    8. jpa:
    9. hibernate:
    10. ddl-auto: update
    11. generate-ddl: true
    12. show-sql: true

    在启动类上加入 vm中 -Djasypt.encryptor.password=salt20220907

    启动还是一样成功,这样就十分安全了。 

    二、Jasypt3.0及以下版本

    此时记住,生成密文的代码改变了,因为3.0以下默认用的是PBEWithMD5AndDES加密,而3.0及以上用的是PBEWITHHMACSHA512ANDAES_256加密。

    比如当版本是2.1.0时

    加密代码将变成如下

    1. public static void main(String[] args) {
    2. StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
    3. // 默认是PBEWithMD5AndDES加密
    4. // 盐值
    5. standardPBEStringEncryptor.setPassword("salt20220907");
    6. // 加密明文
    7. String encryptValue1 = standardPBEStringEncryptor.encrypt("shenyun");
    8. String encryptValue2 = standardPBEStringEncryptor.encrypt("shenyun20220907");
    9. // 解密密文
    10. String decryptValue = standardPBEStringEncryptor.decrypt("1PcipFvXoDg95eW3ZP1MOw==");
    11. System.out.println(encryptValue1);
    12. System.out.println(encryptValue2);
    13. System.out.println(decryptValue);
    14. }

    其它步骤都和上面一样

    千万千万注意,如果生成密文和版本错乱,那么可能会抛出下面错误!!!

    1. Description:
    2. Failed to bind properties under 'spring.datasource.password' to java.lang.String:
    3. Reason: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.password' to java.lang.String
    4. Action:
    5. Update your application's configuration

    可以参考我另外一篇文章:Spring Boot Jasypt 3.0.4 报错---算法加解密使用不一致

  • 相关阅读:
    K_A04_003 基于单片机驱动COG12864显示图片文字和字符串
    Windows环境将SpringBoot程序注册成为服务实现开机自启和后台运行
    Go语言中的值传递和引用传递 附: 内存地址分析
    7、【Qlib】【主要组件】Data Layer:数据框架与使用
    人工智能(AI)基础知识学习库
    Win10安装DBeaver连接MySQL8、导入和导出数据库详细教程
    生成网络之Flow-based Generative Model
    微信小程序:超火的云开发月老办事处月老相亲盲盒
    希望科怀早日康复
    1-十六烷基-3-三乙氧基丙基硅烷咪唑溴盐离子液体([HDTIm]Br)修饰磁性纳米颗粒(MNPs)|[HDTIm]Br-MNPs
  • 原文地址:https://blog.csdn.net/qq_41055045/article/details/126740841