当前位置: 首页 > news >正文

Jasypt 实现 yml 配置加密

文章目录

  • 前言
  • 一、集成 Jasypt
    • 1. pom 依赖
    • 2. yml 依赖
  • 3. 加密工具类
  • 3. 使用
  • 二、常见问题
    • 1. application.yml 失效问题
    • 2. 配置热更新失败问题


前言

jasypt 官方地址:https://github.com/ulisesbocchio/jasypt-spring-boot

Jasypt可以为Springboot加密的信息很多,主要有:

  1. System Property 系统变量。
  2. Envirnment Property 环境变量。
  3. Command Line argument 命令行参数。
  4. Application.properties 应用配置文件。
  5. Yaml properties 应用配置文件。
  6. other custom property sources 其它配置文件。

一、集成 Jasypt

1. pom 依赖

<!-- jasypt(yml加密) -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.5</version>
</dependency>

2. yml 依赖

jasypt:encryptor:password: encpassword # 加密盐值(自定义)algorithm: PBEWithMD5AndDES # 加密算法(3.0.5以下版本默认PBEWithMD5AndDES,即DES加密算法-32位密文;3.0.5及以上版本默认PBEWITHHMACSHA512ANDAES_256,即AES加密算法-64位密文)iv-generator-classname: org.jasypt.iv.NoIvGenerator # 加密偏移生成器

3. 加密工具类

使用如下工具类,对密码进行加密获取密文。

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;/*** jasypt(yml加密)工具** @author whiteen* @date 2024-06-20 00:00:00*/
@Slf4j
public class JasyptUtil {/*** DES加密** @param original 待加密内容* @param password 加密盐值* @return 加密密文* @author whiteen* @date 2024-06-20 00:00:00*/public static String encryptByDes(String original, String password) {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();// 设置盐值config.setPassword(password);config.setAlgorithm("PBEWithMD5AndDES");config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");encryptor.setConfig(config);// 加密return encryptor.encrypt(original);}/*** AES加密** @param original 待加密内容* @param password 加密盐值* @return 加密密文* @author whiteen* @date 2024-06-20 00:00:00*/public static String encryptByAes(String original, String password) {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();// 设置盐值config.setPassword(password);config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");encryptor.setConfig(config);// 加密return encryptor.encrypt(original);}/*** DES解密** @param original 待解密内容* @param password 加密盐值* @return 加密明文* @author whiteen* @date 2024-06-20 00:00:00*/public static String decryptByDes(String original, String password) {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();// 设置盐值config.setPassword(password);config.setAlgorithm("PBEWithMD5AndDES");config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");encryptor.setConfig(config);// 解密return encryptor.decrypt(original);}/*** AES解密** @param original 待解密内容* @param password 加密盐值* @return 加密明文* @author whiteen* @date 2024-06-20 00:00:00*/public static String decryptByAes(String original, String password) {StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();// 设置盐值config.setPassword(password);config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");encryptor.setConfig(config);// 解密return encryptor.decrypt(original);}public static void main(String[] args) {// 待加密内容String original = "original";// 加密盐值String password = "encpassword";// DES加密String encryptedDes = encryptByDes(original, password);log.info("DES密⽂: {}", encryptedDes);// DES解密String decryptedDes = decryptByDes(encryptedDes, password);log.info("DES明⽂: {}", decryptedDes);// AES加密String encryptedAes = encryptByAes(original, password);log.info("AES密⽂: {}", encryptedAes);// AES解密String decryptedAes = decryptByAes(encryptedAes, password);log.info("AES明⽂: {}", decryptedAes);}}

3. 使用

password: ENC(${ORACLE_PWD:password})

二、常见问题

1. application.yml 失效问题

如果你的项目存在 boostrap.yml 配置文件,在引入 jasypt-spring-boot-starter 之后,发现 application.yml 与 application-dev.yml 配置没有生效,需要在 boostrap.yml 中设置 jasypt.encryptor.bootstrap 属性为 false,禁用对 boostrap 配置文件的加密支持,就可以解决 application.yml 与 application-dev.yml 配置失效的问题。

说明:
当 jasypt 和 springcloud 一起使用时,bootstrap 的配置会失效。追踪代码发现,spring 在启动 bootstrap 容器后,当把 bootstrap 容器的 environment 合并到子容器时,只同步了 OriginTrackedMapPropertySource 类型 BootstrapApplicationListener,此时所有的 PropertySource 都已经被包装为 EncryptablePropertySourceWrapper,所以会导致 bootstrap 的配置不会合并到子容器。

2. 配置热更新失败问题

在 yml 配置文件中设置环境变量报错。

参考 apollo 跟 jasypt-spring-boot-2.1.0.jar 不兼容问题: https://github.com/apolloconfig/apollo/issues/2162

升级 jasypt-spring-boot-starter 到 3.0.5 及以上版本可以解决配置热更新问题,再新增 algorithm 和 iv-generator-classname 两个配置即可:

<!-- jasypt(yml加密) -->
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.5</version>
</dependency>
jasypt:encryptor:password: encpassword # 加密盐值(自定义)algorithm: PBEWithMD5AndDES # 加密算法(3.0.5以下版本默认PBEWithMD5AndDES,即DES加密算法-32位密文;3.0.5及以上版本默认PBEWITHHMACSHA512ANDAES_256,即AES加密算法-64位密文)iv-generator-classname: org.jasypt.iv.NoIvGenerator # 加密偏移生成器

这样就可以在 yml 配置文件中设置环境变量:

password: ENC(${ORACLE_PWD:BBO5rGF40i+Sg5oG36MT5aEwpdrOe5f2})
http://www.lryc.cn/news/480153.html

相关文章:

  • uniapp—android原生插件开发(2原生插件开发)
  • NLP之ASR之moonshine:moonshine的简介、安装和使用方法、案例应用之详细攻略
  • albert模型实现微信公众号虚假新闻分类
  • OceanBase 应用实践:如何处理数据空洞,降低存储空间
  • 计算机的错误计算(一百四十八)
  • MySQL记录锁、间隙锁、临键锁(Next-Key Locks)详解
  • SLM401A系列42V商业照明线性恒流芯片 线性照明调光在LED模组及灯带智能球泡灯上应用
  • 京东零售推荐系统可解释能力详解
  • 蓝桥杯 懒洋洋字符串--字符串读入
  • SDL打开YUV视频
  • 微服务架构面试内容整理-Archaius
  • 实现 Nuxt3 预览PDF文件
  • udp为什么会比tcp 有更低的延迟
  • 基于java+SpringBoot+Vue的洗衣店订单管理系统设计与实现
  • HarmonyOS-消息推送
  • 数据分析:宏基因组DESeq2差异分析筛选差异物种
  • 出海企业如何借助云计算平台实现多区域部署?
  • 硬件---1电路设计安全要点以及欧姆定律
  • Linux如何更优质调节系统性能
  • 第三十五章 Vue路由进阶之声明式导航(跳转传参)
  • python爬虫自动库DrissionPage保存网页快照mhtml/pdf/全局截图/打印机另存pdf
  • 基于毫米波雷达和TinyML的车内检测、定位与分类
  • 小E的射击训练
  • React的概念以及发展前景如何?
  • PDF生成:全面解析,C# 如何使用iTextSharp库(或其他类似库)生成PDF文档,包括如何将位图图像嵌入PDF中。
  • 如何选择最适合的消息队列?详解 Kafka、RocketMQ、RabbitMQ 的使用场景
  • gitlab项目如何修改主分支main为master,以及可能遇到的问题
  • RRF(Reciprocal Rank Fusion,倒数排序融合)
  • 移动开发(七):.NET MAUI使用RESTAPI实现查询天气笔记
  • 企业数据无缝对接:从旺店通到金蝶云的入库单管理案例