网站投放广告教程,北京南站最新消息,担保网站建设,郑州搭建网站公司文章目录 前言一、集成 Jasypt1. pom 依赖2. yml 依赖 3. 加密工具类3. 使用二、常见问题1. application.yml 失效问题2. 配置热更新失败问题 前言
jasypt 官方地址#xff1a;https://github.com/ulisesbocchio/jasypt-spring-boot
Jasypt可以为Springboot加密的信息很多https://github.com/ulisesbocchio/jasypt-spring-boot
Jasypt可以为Springboot加密的信息很多主要有
System Property 系统变量。Envirnment Property 环境变量。Command Line argument 命令行参数。Application.properties 应用配置文件。Yaml properties 应用配置文件。other custom property sources 其它配置文件。
一、集成 Jasypt
1. pom 依赖
!-- jasyptyml加密 --
dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion3.0.5/version
/dependency2. 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;/*** jasyptyml加密工具** 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 两个配置即可
!-- jasyptyml加密 --
dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion3.0.5/version
/dependencyjasypt: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:BBO5rGF40iSg5oG36MT5aEwpdrOe5f2})