在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件

这篇具有很好参考价值的文章主要介绍了在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

记录:424

场景:在Spring Boot微服务,使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。

版本:JDK 1.8,Spring Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。

开源地址:https://github.com/ulisesbocchio/jasypt-spring-boot

1.在Spring Boot微服务使用jasypt-spring-boot-3.0.5版本

1.1在pom.xml引入依赖包

(1)依赖包

<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>3.0.5</version>
</dependency>

(2)解析

jasypt-spring-boot-3.0.5底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。

1.2在application.yml中添加jasypt配置

(1)配置

在application.yml中添加jasypt配置,主要是指定加密秘钥。

jasypt:
  encryptor:
    password: U3buwRJdQ2023

(2)解析

jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。

1.3在yml中配置已加密的信息

示例hub.example.password的值已经加密。

hub:
  example:
    password: ENC(C7KjxXpxXC/a/q1R8yCB+xkRIiHnDrDsmB8mEg3AWTvDNCf3nKiV09oZwHIS3SY9Sw1p3JfY3Ed7aWFEnVZ0rg==)

1.4在yml中配置已加密的信息存放格式

使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。

1.5启动微服务

微服务日志:

String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWITHHMACSHA512ANDAES_256
Encryptor config not found for property jasypt.encryptor.key-obtention-iterations, using default value: 1000
Encryptor config not found for property jasypt.encryptor.pool-size, using default value: 1
Encryptor config not found for property jasypt.encryptor.provider-name, using default value: null
Encryptor config not found for property jasypt.encryptor.provider-class-name, using default value: null
Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator
Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.RandomIvGenerator
Encryptor config not found for property jasypt.encryptor.string-output-type, using default value: base64

从日志中可以看出:

jasypt.encryptor.algorithm:PBEWITHHMACSHA512ANDAES_256。

jasypt.encryptor.salt-generator-classname: org.jasypt.salt.RandomSaltGenerator。

jasypt.encryptor.iv-generator-classname: org.jasypt.iv.RandomIvGenerator。

jasypt.encryptor.string-output-type: base64。

1.6测试

代码:

@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
  @Value("${hub.example.password}")
  private String cusPassword;
  @GetMapping("/load01")
  public Object load01() {
      log.info("测试开始...");
      log.info("从yml文件中获取hub.example.password=" + cusPassword);
      log.info("测试结束...");
      return "执行成功";
  }
}

解析:

使用注解获取的@Value("${hub.example.password}")属性值,是已经解密后的内容。

2.在Spring Boot微服务使用jasypt-spring-boot-2.1.2版本

2.1在pom.xml引入依赖包

(1)依赖包

<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>2.1.2</version>
</dependency>

(2)解析

jasypt-spring-boot-2.1.2底层使用jasypt-1.9.3。在引入jasypt-spring-boot后,相关依赖包会自动引入。

2.2在application.yml中添加jasypt配置

(1)配置

在application.yml中添加jasypt配置,主要是指定加密秘钥。

jasypt:
  encryptor:
    password: U3buwRJdQ2023

(2)解析

jasypt-spring-boot会拿着指定的秘钥对需要解密的配置信息解密。

2.3在yml中配置已加密的信息

示例hub.example.password的值已经加密。

hub:
  example:
    password: ENC(/BxyrksOnj3U/HCwkRVySHRZs2s4eZveCVncPoCzHMI=)

2.4在yml中配置已加密的信息存放格式

使用jasypt-spring-boot加密的信息,在yml文件中使用ENC()包裹起来。

2.5启动微服务

微服务日志:

String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
Encryptor config not found for property jasypt.encryptor.algorithm, using default value: PBEWithMD5AndDES
Encryptor config not found for property jasypt.encryptor.key-obtention-iterations, using default value: 1000
Encryptor config not found for property jasypt.encryptor.pool-size, using default value: 1
Encryptor config not found for property jasypt.encryptor.provider-name, using default value: null
Encryptor config not found for property jasypt.encryptor.provider-class-name, using default value: null
Encryptor config not found for property jasypt.encryptor.salt-generator-classname, using default value: org.jasypt.salt.RandomSaltGenerator
Encryptor config not found for property jasypt.encryptor.iv-generator-classname, using default value: org.jasypt.iv.NoIvGenerator
Encryptor config not found for property jasypt.encryptor.string-output-type, using default value: base64

从日志中可以看出:

jasypt.encryptor.algorithm:PBEWithMD5AndDES。

jasypt.encryptor.salt-generator-classname: org.jasypt.salt.RandomSaltGenerator。

jasypt.encryptor.iv-generator-classname: org.jasypt.iv.NoIvGenerator。

jasypt.encryptor.string-output-type: base64。

2.6测试

代码:

@RestController
@RequestMapping("/hub/example/city")
@Slf4j
public class CityController {
  @Value("${hub.example.password}")
  private String cusPassword;
  @GetMapping("/load01")
  public Object load01() {
      log.info("测试开始...");
      log.info("从yml文件中获取hub.example.password=" + cusPassword);
      log.info("测试结束...");
      return "执行成功";
  }
}

解析:

使用注解获取的@Value("${hub.example.password}")属性值,是已经解密后的内容。

3.生成加密配置

在yml文件中配置加密属性时,需先生成加密配置信息,并使用ECN()包裹起来放入yml配置中。

public class JasyptDemo {
   public static void main(String[] args) {
     f1_BasicTextEncryptor();
     f2_AES256TextEncryptor();
   }
  /**
   * 加密工具类: org.jasypt.util.text.BasicTextEncryptor
   * 加密算法: PBEWithMD5AndDES
   */
  public static void f1_BasicTextEncryptor() {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    System.out.println("当前加密方式: 加密类: BasicTextEncryptor, 加密算法: PBEWithMD5AndDES ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
  }
  
  /**
   * 加密工具类: org.jasypt.util.text.AES256TextEncryptor
   * 加密算法: PBEWithHMACSHA512AndAES_256
   */
  public static void f2_AES256TextEncryptor() {
    AES256TextEncryptor textEncryptor = new AES256TextEncryptor();
    System.out.println("当前加密方式: 加密类: AES256TextEncryptor, 加密算法: PBEWithHMACSHA512AndAES_256 ");
    // 1.设置秘钥
    String salt = "U3buwRJdQ2023";
    textEncryptor.setPassword(salt);
    // 2.加密
    // 2.1加密内容
    String pd = "Hangzhou20230427";
    System.out.println("加密前:  " + pd);
    // 2.2加密操作
    String pdAfterEncrypt = textEncryptor.encrypt(pd);
    System.out.println("加密后:  " + pdAfterEncrypt);
    // 3.解密操作
    String pdAfterDecrypt = textEncryptor.decrypt(pdAfterEncrypt);
    System.out.println("解密后:  " + pdAfterDecrypt);
}
}

4.jasypt-spring-boot的版本2.1.2和3.0.5比对

4.1相同点

底层都使用jasypt-1.9.3。

4.2不同点

默认使用算法不同。

jasypt-spring-boot-2.1.2默认加密算法:PBEWithMD5AndDES。

jasypt-spring-boot-3.0.5默认加密算法:PBEWithHMACSHA512AndAES_256。

因此,从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,需要做适当修改。比如重新生成加密配置信息。或者指定算法。

5.jasypt-spring-boot的版本从2.1.2切换到3.0.5注意事项

从jasypt-spring-boot-2.1.2切换到jasypt-spring-boot-3.0.5时,在不修改版2.1.2生成的加密信息时,则需在yml中指定算法信息。

报错:

Unable to decrypt property: ... Decryption of Properties failed,  make sure encryption/decryption passwords match

原因:

jasypt-spring-boot-2.1.2和jasypt-spring-boot-3.0.5默认使用加密和解密算法不一样,因此在使用jasypt-spring-boot-2.1.2时,生成的加密信息,切换到jasypt-spring-boot-3.0.5无法解密。

解决:

使用jasypt-spring-boot-3.0.5时,在yml文件中指定算法,不使用默认算法就行。

jasypt:
  encryptor:
    password: U3buwRJdQ2023
	algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

以上,感谢。

2023年4月27日文章来源地址https://www.toymoban.com/news/detail-485563.html

到了这里,关于在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot项目使用 jasypt 加密组件进行加密(例如:数据库、服务的Key、等等进行加密)

    🍓 简介:java系列技术分享(👉持续更新中…🔥) 🍓 初衷:一起学习、一起进步、坚持不懈 🍓 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正🙏 🍓 希望这篇文章对你有所帮助,欢迎点赞 👍 收藏 ⭐留言 📝 🍓 更多文章请点击 密码配置项都不加密? 想啥呢? 一

    2024年02月07日
    浏览(41)
  • 在Spring Boot微服务使用ListOperations操作Redis集群List列表

    记录 :444 场景 :在Spring Boot微服务使用RedisTemplate的ListOperations操作Redis集群的List列表数据类型。 版本 :JDK 1.8,Spring Boot 2.6.3,redis-6.2.5。 1.微服务中 配置Redis信息 1.1在pom.xml添加依赖 pom.xml文件: 解析:spring-boot-starter-data-redis和spring-boot版本保持一致。 1.2在application.yml中配置

    2024年02月08日
    浏览(35)
  • spring boot集成jasypt 并 实现自定义加解密

    由于项目中的配置文件 配置的地方过多,现将配置文件统一放到nacos上集中管理 且密码使用加密的方式放在配置文件中 项目中组件使用的版本环境如下 spring cloud 2021.0.5 spring cloud alibaba 2021.0.5.0 spring boot 2.6.13 配置文件的加密使用 加密库 jasypt 引入maven依赖 添加配置 使用jasy

    2024年02月11日
    浏览(32)
  • 在Spring Boot微服务使用ZSetOperations操作Redis集群Zset(有序集合)

    记录 :447 场景 :在Spring Boot微服务使用RedisTemplate的ZSetOperations操作Redis集群的Zset(有序集合)数据类型。 版本 :JDK 1.8,Spring Boot 2.6.3,redis-6.2.5。 1.微服务中 配置Redis信息 1.1在pom.xml添加依赖 pom.xml文件: 解析:spring-boot-starter-data-redis和spring-boot版本保持一致。 1.2在application.ym

    2024年02月08日
    浏览(33)
  • 在Spring Boot微服务使用JedisCluster操作Redis集群String字符串

    记录 :449 场景 :在Spring Boot微服务使用JedisCluster操作Redis集群的String字符串数据类型。 版本 :JDK 1.8,Spring Boot 2.6.3,redis-6.2.5,jedis-3.7.1。 1.微服务中 配置Redis信息 1.1在pom.xml添加依赖 pom.xml文件: 解析:在Spring Boot中默认集成jedis,使用无需加版本号,本例版本3.7.1是Spring Bo

    2024年02月08日
    浏览(37)
  • SpringBoot+jasypt-spring-boot-starter实现配置文件明文加密

    springboot:2.1.4.RELEASE JDK:8 jasypt-spring-boot-starter:3.0.2 Jasypt默认算法为PBEWithMD5AndDES,该算法需要一个加密密钥,可以在应用启动时指定(环境变量)。也可以直接写入配置文件 3.1 application.properties配置文件版 加密后,可删除jasypt.encryptor.password配置;发版时可在命令行中配置 3.2 函数

    2024年02月15日
    浏览(28)
  • Spring Boot学习随笔- Jasypt加密数据库用户名和密码以及解密

    学习视频:【编程不良人】2021年SpringBoot最新最全教程 Jasypt 全称是Java Simplified Encryption,是一个开源项目。 Jasypt与Spring Boot集成,以便在应用程序的属性文件中加密敏感信息,然后在应用程序运行时解密这些信息。 可以使用 jasypt-spring-boot-starter 这个依赖项。从而实现属性源

    2024年02月04日
    浏览(53)
  • 《深入浅出Spring Boot 3.x》已经交稿,目前在编写《Spring Cloud Alibaba微服务实践》

    最近基于《深入浅出Spring Boot 2.x》升级为《深入浅出Spring Boot 3.x》已经完成,预计明年春节后可以出版,我先贴出目录给大家看看。以后希望大家多多支持。 第1章   Spring Boot 3.x的来临 1.1  Spring的历史 1.2  Spring Boot的特点 1.3 Spring和Spring Boot的关系 1.4 开发Spring Boot工程 第2章

    2024年02月09日
    浏览(28)
  • 【Spring Boot】使用Spring Boot进行transformer的部署与开发

    Transformer是一个用于数据转换和处理的平台,使用Spring Boot可以方便地进行Transformer的部署与开发。 以下是使用Spring Boot进行Transformer部署与开发的步骤: 创建Spring Boot项目 可以使用Spring Initializr创建一个简单的Spring Boot项目。在创建项目时,需要添加以下依赖: 编写Transforme

    2024年02月11日
    浏览(27)
  • 【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理

    💓 博客主页:从零开始的-CodeNinja之路 ⏩ 收录文章:【Spring Boot】掌握Spring Boot:深入解析配置文件的使用与管理 🎉欢迎大家点赞👍评论📝收藏⭐文章 配置文件主要是为了解决硬编码带来的问题,把可能会发生改变的信息,放在⼀个集中的地方,当我们启 动某个程序时,应用程

    2024年04月23日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包