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

这篇具有很好参考价值的文章主要介绍了加密组件Jasypt学习、实战及踩坑记录。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

最近入职新公司,因几乎全部项目都使用到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();
        // 加密所需的salt
        textEncryptor.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
加密组件Jasypt学习、实战及踩坑记录
添加依赖即可:

<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也一直在提示我:
加密组件Jasypt学习、实战及踩坑记录
故而想着升级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可以解决配置热更新问题,新增两个配置即可:文章来源地址https://www.toymoban.com/news/detail-414971.html

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

参考

到了这里,关于加密组件Jasypt学习、实战及踩坑记录的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • Flask结合gunicorn和nginx反向代理的生产环境部署及踩坑记录

    个人博客:https://xzajyjs.cn 之前自己写的flask使用gunicorn上线生产环境没有什么问题,但是最近搭建了一个现成的flask项目,当使用python直接运行时不会有问题,而使用gunicorn时则会出现一些问题。 这里使用pyenv创建了一个虚拟环境,并安装好依赖 下面是入口函数 run.py : 其中

    2024年02月16日
    浏览(31)
  • Spring Boot项目使用 jasypt 加密组件进行加密(例如:数据库、服务的Key、等等进行加密)

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

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

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

    2024年02月04日
    浏览(50)
  • Zabbix【部署 05】 Docker部署Zabbix Server Agent Agent2 Web interface及 Java-Gate-Way(详细启动脚本及踩坑记录)不定时更新

    安装Docker。 为Zabbix创建专用网络 zabbix-net 。 创建数据库用户 zabbix 和数据库 zabbix 。 172.20.240.0【Zabbix 专用网络的 Gateway IP 下边有查询方法】 172.17.0.8【云服务器的内网IP】 开启JAVAGATEWAY服务: 2.1 agent 其他启动命令: 踩坑记录: 2.2 agent2 Zabbix agent 2 是新一代的 Zabbix agent,可以

    2024年02月02日
    浏览(25)
  • oracle 重启步骤及踩坑经验

    oracle 重启步骤及踩坑经验 切换到oracle用户 关闭监听 杀掉oracle有关进程 登录数据库,关闭oracle数据库 重启监听 重启数据库 shutdown immediate 时可能比较久,此时不要着急退出,等待一会等他报错 重启oracle startup时一直卡在Database mounted 可以等一会,如果等了很久没报错就退出

    2024年02月06日
    浏览(28)
  • ElasticSearch 7.6.2版本集群搭建及踩坑

    服务器说明 本次演示采用三台RockyLinux 8.5版本服务器 服务器 IP 备注 es-master 172.16.7.11 主节点 es-node01 172.16.7.5 01节点 es-node02 172.16.7.13 02节点 内核版本 修改系统名 配置hosts 所有服务器配置一些/etc/hosts 安装JDK 创建用户 下载软件 ES中文社区下载连接: 挑一个自己觉得稳定的版本

    2023年04月27日
    浏览(42)
  • Windows10下docker安装指南及踩坑解决

    1.官网下载docker文件( 注意:Windows10家庭版要自己安装hyper-v,专业版以上自带) 顺利启动docker界面 虚拟化技术没打开,需要打开虚拟化hyper 解决:打开设置下的控制面板-程序和功能-启用和关闭windows功能-勾选打开hyper-v 备注:如遇hyper-v平台置灰无法打开,需要开启bios中的虚拟

    2024年02月04日
    浏览(33)
  • Spring Cloud OpenFeign 的使用及踩坑指南

    Feign 和OpenFeign Feign OpenFeign openFeign的优势 OpenFeign应用 1. 导入依赖 2. 使用 3. 日志配置 4. 数据压缩 OpenFeign高级应用 OpenFeign熔断降级的两种方式-降级方法和降级工厂 踩坑指南 坑一:Http Client 坑二:全局超时时间 坑三:单服务设置超时时间 遇到的问题 1. 使用Spring MVC注解,但请

    2024年02月14日
    浏览(31)
  • 【其他】Windows10下docker安装指南及踩坑解决

    1.官网下载docker文件( 注意:Windows10家庭版要自己安装hyper-v,专业版以上自带) 顺利启动docker界面 虚拟化技术没打开,需要打开虚拟化hyper 解决:打开设置下的控制面板-程序和功能-启用和关闭windows功能-勾选打开hyper-v 备注:如遇hyper-v平台置灰无法打开,需要开启bios中的虚拟

    2024年02月06日
    浏览(40)
  • 【Jasypt】Spring Boot 配置文件加解密 Jasypt 配置文件加密

    Jasypt是一个Java简易加密库,用于加密配置文件中的敏感信息,如数据库密码。jasypt库与springboot集成,在实际开发中非常方便。 1、Jasypt Spring Boot 为 spring boot 应用程序中的属性源提供加密支持,出于安全考虑,Spring boot 配置文件中的敏感信息通常需要对它进行加密/脱敏处理,

    2024年02月03日
    浏览(31)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包