0


SpringBoot【集成 jasypt】实现配置信息自定义加解密(自定义的属性探测和密码解析器)

集成jasypt实现配置信息自定义加解密

1.Jasypt是什么

Jasypt是一个Java简易加密库,用于加密配置文件中的敏感信息,如数据库密码。它可以帮助开发人员在应用程序中加密密码、敏感信息和数据通信,还包括高安全性、基于标准的加密技术、可同时单向和双向加密的加密密码、文本、数字和二进制文件。如果您正在使用Spring Boot,Jasypt可以与Spring Boot集成,使加密和解密过程更加简单。

2.使用

2.1 依赖

<!-- SpringBoot 版本 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.3</version><relativePath/></parent><!-- jasypt 加密 --><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.3</version></dependency>

2.2 实现类

  1. 自定义的属性探测器和密码解析器【作用是识别加密后的对象和解密】
/**
 * 自定义的属性探测器和密码解析器
 */@ComponentpublicclassCustomEncryptableProperty{@Bean(name ="encryptablePropertyDetector")publicEncryptablePropertyDetectorencryptablePropertyDetector(){returnnewCustomEncryptablePropertyDetector();}@Bean("encryptablePropertyResolver")publicEncryptablePropertyResolverencryptablePropertyResolver(EncryptablePropertyDetector encryptablePropertyDetector){returnnewCustomEncryptablePropertyResolver(encryptablePropertyDetector);}}
  1. 自定义的属性探测器实现【可以设置被加密对象】
/**
 * 自定义的属性探测器
 */publicclassCustomEncryptablePropertyDetectorimplementsEncryptablePropertyDetector{/**
     * 探测字符串
     */privatefinalString flagStr ="ENC@";/**
     * 是否为可以解密的字符串【自定义规则为 flagStr 开头】
     *
     * @param value 全部的字符串
     * @return 是否是解密的字符串,true,是,false,否
     */@OverridepublicbooleanisEncrypted(String value){if(value !=null){return value.startsWith(flagStr);}returnfalse;}/**
     * 截取到除了标识之后的值【截取 flagStr 之后的字符串】
     *
     * @param value 带前缀
     * @return string 去掉标识符的字符串
     */@OverridepublicStringunwrapEncryptedValue(String value){return value.substring(flagStr.length());}}
  1. 自定义的密码解析器【解密自定义的加密对象】EncryptionUtil.toDecrypt()就是自定义的解密方法,与加密对象的加密方法相对应。
/**
 * 自定义的密码解析器
 */publicclassCustomEncryptablePropertyResolverimplementsEncryptablePropertyResolver{/**
     * 属性探测器
     */privatefinalEncryptablePropertyDetector detector;publicCustomEncryptablePropertyResolver(EncryptablePropertyDetector detector){this.detector = detector;}/**
     * 处理真正的解密逻辑
     *
     * @param value 原始值
     * @return 如果值未加密,返回原值,如果加密,返回解密之后的值
     */@OverridepublicStringresolvePropertyValue(String value){returnOptional.ofNullable(value).filter(detector::isEncrypted).map(resolvedValue ->{try{// 1.过滤加密规则后的字符串String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim());// 2.解密returnEncryptionUtil.toDecrypt(unwrappedProperty);}catch(EncryptionOperationNotPossibleException e){thrownewDecryptionException("Unable to decrypt: "+ value +". Decryption of Properties failed,  make sure encryption/decryption "+"passwords match", e);}}).orElse(value);}}

2.3 加密配置

spring:datasource:dynamic:datasource:# 主库数据源master:driver-class-name: org.postgresql.Driver
          url: ENC@URLENCStr
          username: ENC@UsernameENCStr
          password: ENC@PasswordENCStr

3.总结

用户名及密码甚至是URL使用密文的安全性是很高的,本文参考知乎

软件架构师:代码小咖

SpringBoot 配置文件这样加密,才足够安全!,感谢大佬的分享,Jasypt 的使用还有很多自定义的方式,可查看原文学习。


本文转载自: https://blog.csdn.net/weixin_39168541/article/details/134433893
版权归原作者 シ風箏 所有, 如有侵权,请联系我们删除。

“SpringBoot【集成 jasypt】实现配置信息自定义加解密(自定义的属性探测和密码解析器)”的评论:

还没有评论