欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

SpringBoot [与 jasypt 集成],实现配置信息的自定义加密和解密(自定义属性检测和密码解析器)

最编程 2024-03-27 22:17:44
...
/** * 自定义的密码解析器 */ public class CustomEncryptablePropertyResolver implements EncryptablePropertyResolver { /** * 属性探测器 */ private final EncryptablePropertyDetector detector; public CustomEncryptablePropertyResolver(EncryptablePropertyDetector detector) { this.detector = detector; } /** * 处理真正的解密逻辑 * * @param value 原始值 * @return 如果值未加密,返回原值,如果加密,返回解密之后的值 */ @Override public String resolvePropertyValue(String value) { return Optional.ofNullable(value) .filter(detector::isEncrypted) .map(resolvedValue -> { try { // 1.过滤加密规则后的字符串 String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim()); // 2.解密 return EncryptionUtil.toDecrypt(unwrappedProperty); } catch (EncryptionOperationNotPossibleException e) { throw new DecryptionException("Unable to decrypt: " + value + ". Decryption of Properties failed, make sure encryption/decryption " + "passwords match", e); } }) .orElse(value); } }