加密组件Jasypt学习、实战及踩坑记录

article/2025/10/9 0:48:37

概述

最近入职新公司,因几乎全部项目都使用到jasypt,故而初步学习记录下本文(持续更新)。
官网及GitHub给出的简介:使用简单,性能好,特性features非常丰富;支持

另,有个开源Jasypt-spring-boot组件,GitHub,集成Jasypt,方便Spring Boot开发者使用。

实战

开发中最常见的场景就是对数据库的密码进行加密,将加密后的密码配置在Apollo等配置中心。

Jasypt

使用Jasypt需引入如下依赖:

<dependency><groupId>org.jasypt</groupId><artifactId>jasypt</artifactId><version>1.9.3</version>
</dependency>

而数据库的密码,不要使用=root、1qaz2wsx这种极易被暴力破解的。

推荐使用在线随机密码生成器,可指定密码的位数,当然也支持指定是否包括大、小写字母、数字、特殊符号等。

借助于这个在线工具,生产随机密码FkSs3k31

直接上代码,Jasypt工具类:

@Slf4j
public class JasyptUtil {public static void main(String[] args) {String pass = "FkSs3k31";String salt = "johnny";int n = 3;String[] en = new String[n];String[] de = new String[n];for (int i = 0; i < n; i++) {en[i] = encrypt(salt, password);de[i] = decrypt(salt, en[i]);log.info("加密: " + en[i] + ",解密:" + de[i] + ",相等=" + password.equals(de[i]));}}public static String encrypt(String passWord, String message) {BasicTextEncryptor textEncryptor = new BasicTextEncryptor();// 加密所需的salttextEncryptor.setPassword(passWord);return textEncryptor.encrypt(message);}public static String decrypt(String passWord, String encryptor) {BasicTextEncryptor textEncryptor = new BasicTextEncryptor();textEncryptor.setPassword(passWord);return textEncryptor.decrypt(encryptor);}
}

打印输出:

加密: G1XGWDxvGiOcDidkDTFnceNJCDr+SxPE,解密:FkSs3k31,相等=true
加密: lpLDMbDmfspxHXm0n62d1ekJin9KGwkI,解密:FkSs3k31,相等=true
加密: CvUIJ5/HU0Z201j0wDH613Z1y+445Pxu,解密:FkSs3k31,相等=true

可见每次加密得到的密码都不尽相同,但是都能够成功解密。
至于为什么要判断一下原文和解密后的是否相等,参考Java String加解密踩坑

Jasypt-spring-boot

使用Jasypt-spring-boot更是简单方便。通过前面的JasyptUtil,得到一系列数据库不同schema的加密后密码,增加前缀ENC(和后缀),放在配置中心;然后配置中心再新增一个键值对:jasypt.encryptor.password=johnny
在这里插入图片描述
添加依赖即可:

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

Spring Boot应用启动时,JasyptSpringBootAutoConfiguration会自动扫描配置类(不管是放在yml本地文件还是配置中心的),检查前缀和后缀,根据配置的加密password去解密。

应用启动打印信息:

分析

引入上述依赖后,EncryptablePropertyResolverConfiguration配置类检查前缀和后缀,

{"name": "jasypt.encryptor.property.prefix","type": "java.lang.String","description": "Specify a custom {@link String} to identify as prefix of encrypted properties. Default value is {@code \"ENC(\"}","sourceType": "com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties$PropertyConfigurationProperties","defaultValue": "ENC("
},
{"name": "jasypt.encryptor.property.suffix","type": "java.lang.String","description": "Specify a custom {@link String} to identify as suffix of encrypted properties. Default value is {@code \")\"}","sourceType": "com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties$PropertyConfigurationProperties","defaultValue": ")"
}

进阶

Jasypt

Jasypt-spring-boot

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

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

问题

FileNotFoundException

Caused by: java.io.FileNotFoundException: class path resource [com/ulisesbocchio/jasyptspringboot/configuration/EnableEncryptablePropertiesConfiguration.class] cannot be opened because it does not exist

DecryptionException: Unable to decrypt property

应用启动失败,完整的报错信息如下:

| ERROR | org.springframework.boot.SpringApplication | reportFailure | 821 | - 
Application run failed
com.ulisesbocchio.jasyptspringboot.exception.DecryptionException: Unable to decrypt property: ENC() resolved to: ENC(). Decryption of Properties failed,  make sure encryption/decryption passwords match
at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.lambda$resolvePropertyValue$0(DefaultPropertyResolver.java:63)

解密失败?第一反应就是拿着这个报错的密文使用上面的工具类解密,解密成功!

参考stackoverflow,排查下来和jasypt-spring-boot-starter版本有关。

发布前版本为1.18,因应用两年多没有人维护,IDEA也一直在提示我:
在这里插入图片描述
故而想着升级pom文件里的诸多依赖,随手顺带把jasypt-spring-boot-starter的依赖升级到最新版3.0.5

参考maven repository,1.18版本后的几个主要(大)版本:2.0.02.1.03.0.0

解决方案:

  1. 版本回退到2.1.0:1.18->3.0.5->2.1.0
  2. 增加配置:
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

验证下来,两种方案都可行,按需选择一种即可。

Failed to bind properties under ‘’ to java.lang.String

也是一个应用启动(发布)失败的问题,报错日志:

org.springframework.context.ApplicationContextException: Unable to start web server;
nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicatorRegistry]: Factory method 'healthIndicatorRegistry' threw exception;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'spring.data.mongodb-org.springframework.boot.autoconfigure.mongo.MongoProperties': Could not bind properties to 'MongoProperties' : prefix=spring.data.mongodb, ignoreInvalidFields=false, ignoreUnknownFields=true; 
nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.data.mongodb.uri' to java.lang.String

简单来说,应用启动时最底层报错为:Failed to bind properties under 'spring.data.mongodb.uri' to java.lang.String

应用使用的版本为2.1.0,

参考stackoverflow

下降到2.0.0版本解决问题。

配置热更新失败

使用的 jasypt-spring-boot-starter 版本为2.1.0,apollo-client版本为1.5.0。遇到的问题,在Apollo Server端修改配置并发布后,配置不能实现热更新。

参考apollo跟jasypt-spring-boot-2.1.0.jar不兼容。

解决方法:升级jasypt-spring-boot-starter。经过验证,升级到3.0.5可以解决配置热更新问题,新增两个配置即可:

jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

参考


http://chatgpt.dhexx.cn/article/JGQEFWP2.shtml

相关文章

Jasypt加解密

Jasypt加密 Jasypt加密引言介绍整合SpringBoot引入依赖编写配置加密和解密通过Java程序获取密文和解密通过jasypt中jar包程序获取密文使用密文&#xff1a;ENC(密文) 异常问题JCE权限问题yml中带有引起的问题 关于盐值&#xff08;密钥&#xff09;配置设置其他 Jasypt加密 引…

微服务SpringBoot整合Jasypt加密工具

文章目录 一、Jasypt介绍二、Jasypt手动使用2.1 密码加密场景2.2 文本加密场景2.3 数值加密场景2.4 二进制数据加密场景 三、Jasypt整合SpringBoot四、生成环境启动无、参考文档 一、Jasypt介绍 Jasypt是Java加密工具包&#xff0c;能支持对密码的哈希加密&#xff0c;对文本和…

Springboot之Jasypt配置文件加密/解密

Jasypt配置文件加密/机密 一、Jasypt介绍二、Springboot整合Jasypt2.1 环境配置2.2 添加依赖2.3 添加Jasypt配置2.4 编写加/解密工具类2.5 修改配置文件2.5 如何进一步防止密码泄露2.5.1 自定义加密器2.5.3 加密盐值通过环境变量指定 文章系列 【一、Springboot之Jasypt配置文…

java中byte与int的转换原理

前些天遇到一个问题&#xff0c;byte[0] "A3"&#xff08;十六进制表示&#xff09; 但是在debug时显示的是 -93 &#xff0c;而如果直接赋值给int的变量也是-93.当然大部分人都知道这是不能直接赋值的&#xff0c;需要以下面的这种方式将byte转换成int&#xff1a;…

Java byte 转化为 String

1、Java 中 byte 转化为 String&#xff0c;代码如下 package nice.com.mian;import java.io.UnsupportedEncodingException;public class StringMain {public static void main(String[] args) throws Exception {byte[] bb {97,99,105,51,55};String str new String(bb, &…

Java byte[] 转 String的简单转换

简单的将byte[] 还原成了 String 值 [TOC] 代码&#xff1a; public class Test0719 {public static void main(String[] args) {String text "test";byte[] textBytes text.getBytes();String content byteToString(textBytes);System.out.println(textBytes &q…

java:int强制类型转换成byte

注&#xff1a;非常感谢评论的最佳戏子大佬指出了我的不足&#xff0c;已经进行修改 int强制类型转换成byte 一、基础知识二、int->byte方法一方法二 一、基础知识 int 在java中是32位&#xff0c; byte是8位 原码&#xff1a;就是二进制码&#xff0c;最高位为符号位&…

C#初级编程

Unity学习笔记——C#初级编程 一、Awake 和 Start二、Update 和 FixedUpdate三、启用和禁用组件四、SetActive、Active Self、Active in Hierarchy五、位置和旋转六、transform.lookAt(Transform target)七、线性插值八、Destroy九、GetButton 和 GetKey十、GetAxis十一、GetCo…

零基础怎样自学编程?新手如何学习编程?编程学习入门指南

对于编程&#xff0c;很多新手的第一感觉可能就是&#xff1a; 高深&#xff0c;难学。 学好编程&#xff0c;有的时候&#xff0c;可以把一些需要我们重复劳动的工作&#xff0c;自动化批量处理&#xff0c;为我们节省很多时间和精力。 对于一些学得比较深入的朋友来说&…

新手学计算机编程怎么入门 从哪学起

近些年有一种职业发展很快、人才需求量大、工资高&#xff0c;那就是程序员。他们的基本工作就是电脑编程&#xff0c;开发者各种各样的软件、APP&#xff0c;被很多人膜拜。如果你想成为一名程序员&#xff0c;有必要了解一下这些基本的电脑编程入门教程。 1计算机编程怎么入门…

代码编程教学入门

前言 代码编程是我们在现代能获得的最宝贵的技能之一&#xff0c;学会写代码不仅让我们在职业前景上更得心应手&#xff0c;而且还能让我们的大脑保持活跃和创造性&#xff0c;甚至我们还有机会创造出一些 awesome 的东西出来。 如果我们才刚刚开始&#xff08;或准备开始&am…

20道经典C语言编程题(初级)

编者按&#xff1a;以下20道题目为自己练习所写的代码&#xff0c;均已提交检测通过&#xff0c;其中有些题也许有更优的解法&#xff0c;所以写的不好望读者包涵&#xff0c;由于个人练习&#xff0c;所以没有写注释&#xff0c;有什么问题的地方&#xff0c;可以留言一起交流…

沈师 PTA 数据库题目及部分解析 第四章

判断题 1.在数据库安全性控制中&#xff0c;所有授予出去的权力在必要时都可以用REVOKE语句收回。 T 2.用户可以“自主”地决定将数据的存取权限授予何人、决定是否也将“授权”的权限授予别人&#xff0c;因此称这样的存取控制是自主存取控制。 T 3.数据库的安全性是指保护数据…

编程题目+数据库题目总结(3)

目录 编程题1.字典中保存了一些股票代码&#xff08;键&#xff09;及价格&#xff08;值&#xff09;&#xff0c;用一行代码找出价格最高的股票&#xff0c;输出股票代码。(5分&#xff09;2.字典中保存了一些股票代码&#xff08;键&#xff09;及价格&#xff08;值&#x…

沈师 PTA 数据库题目及部分解析 第一章

判断题&#xff1a; 1.一个数据库只有一个模式和一个内模式。 T 2.数据模型是由数据结构、数据操作和完整性约束三部分组成的 T 3.由于数据库中的数据是不断更新的&#xff0c;因此关系模式是相对变化的。 F 解析&#xff1a;关系模式不会相对变化&#xff0c;变化的是实例 4.数…

【数据库题目复习】期末试卷 一、二

文章目录 试题一一、选择题二、填空题三、简答题四、设计题五、综合题 试题二一、选择题二、填空题三、简答题四、设计题五、综合题 试题一 一、选择题 1. 数据库系统的核心是&#xff08; B &#xff09; A&#xff0e;数据库 B&#xff0e;数据库管理系统 C&#xf…

【数据库题目复习】第7章 数据库设计

文章目录 一、选择题&#xff1a;二、填空三、应用 参考文章&#xff1a;https://blog.csdn.net/qq_46139801/article/details/117453449 知识点&#xff1a; &#xff08;1&#xff09;各子系统的E-R图之间的冲突主要有三类&#xff1a; 属性冲突、命名冲突和结构冲突。 &…

【力扣刷题总结】数据库题目按知识点分类总结

观前提示&#xff1a; 这个图先自行背诵至滚瓜烂熟。 文档中论述题目解题思路时没有特殊情况都按照上面的程序执行顺序为准&#xff0c;个别题也会以其他更自然的顺序讲解。 本文的知识点整理仅起提示作用&#xff0c;一些不常用功能、参数可能会遗漏&#xff0c;如需了解详细…

数据库题目之数据库设计

一、选择题 1、在数据库设计中&#xff0c;用E-R图来描述信息结构但不涉及信息在计算机中的表示&#xff0c;它是数据库设计的 阶段。 A&#xff0e;需求分析 B&#xff0e;概念设计 C&#xff0e;逻辑设计 D&#xff0e;物理设计 【答案&#xff1a;】B 2、在关系…