当前位置: 首页 > 后端技术 > Java

在使用jasypt加密配置的时候,报错:DecryptionException-Unabletodecrypt

时间:2023-04-01 14:30:56 Java

前几天分享了一篇文章《Spring Boot 2.x基础教程:加密配置中的敏感信息》,然后看到群里有小伙伴回复说跟这篇文章后出现了这个异常com.ulisesbocchio.jasyptspringboot.exception.DecryptionException:无法解密。具体完整的错误信息如下:Causedby:com.ulisesbocchio.jasyptspringboot.exception.DecryptionException:Unabletodecrypt:ENC(/AL9nJENCYCh9Pfzdf2xLPsqOZ6HwNgQ3AnMybFAMeOM5GphZlOK6PxzozwtCm+Q).属性解密失败,确保加密/解密密码匹配com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.lambda$resolvePropertyValue$0(DefaultPropertyResolver.java:46)~[jasypt-spring-boot-3.0.3.jar:na]在java.util.Optional.map(Optional.java:215)~[na:1.8.0_151]在com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.resolvePropertyValue(DefaultPropertyResolver.java:40)~[jasypt-spring-boot-3.0.3.jar:na]在com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver.resolvePropertyValue(DefaultLazyPropertyResolver.java:50)~[jasypt-spring-boot-3.0.3.jar:na]在com.ulisesbocchio.jasyptspringboot。EncryptablePropertySource.getProperty(EncryptablePropertySource.java:20)~[jasypt-spring-boot-3.0.3.jar:na]在com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty(CachingDelegateEncryptablePropertySource.java:41)~[jasypt-spring-boot-3.0.3.jar:na]在com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty(EncryptableMapPropertySourceWrapper.java:31)~[jasypt-spring-boot-3.0.3.jar:na]在org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:62)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:588)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:137)~[spring-context-5.3.8.jar:5.3.8]在org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:133)~[spring-context-5.3.8.jar:5.3.8]在org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver。java:74)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:159)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)~[spring-core-5.3.8.jar:5.3.8]在org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175)~[spring-context-5.3.8.jar:5.3.8]在org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1321)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)~[spring-beans-5.3.8.jar:5.3.8]在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)~[spring-beans-5.3.8.jar:5.3.8]...省略了69个常见框架由org.jasypt.exceptions引起。EncryptionOperationNotPossibleException:org.jasypt.encryption.pbe.StandardPBEByteEncryptor.decrypt(StandardPBEByteEncryptor.java:1165)为空~[jasypt-1.9.3.jar:na]在org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:738)~[jasypt-1.9.3.jar:na]在org.jasypt.encryption.pbe.PooledPBEStringEncryptor.decrypt(PooledPBEStringEncryptor.java:511)~[jasypt-1.9.3.jar:na]在com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor.decrypt(DefaultLazyEncryptor.javaringsypt:57)~[ja-boot-3.0.3.jar:na]在com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.lambda$resolvePropertyValue$0(DefaultPropertyResolver.java:44)~[jasypt-spring-boot-3.0.3.jar:na]。..94commonframe省略直接根据报错信息的描述判断,就是解密失败即jasypt.encryptor.password的配置和使用插件时传递的参数不一样。第二种:不安装无限长JCE版本(UnlimitedStrengthJavaCryptographyExtension)。因为之前使用SpringCloudConfig的时候已经安装了DD,所以之前写案例的时候没有提到这个。应该是一些没有接触过SpringCloud的读者造成的。其实这个在之前的文章《分布式配置中心(加密解密)》中有提到过,但是因为不属于系列内容,所以这里再提一下。我们可以从Oracle官网下载你使用的Java版本对应的JCE安装包,例如:JCE8下载地址。它是一个压缩包。解压后可以看到如下三个文件:README.txtlocal_policy.jarUS_export_policy.jar我们需要将local_policy.jar和US_export_policy.jar这两个文件复制到$JAVA_HOME/jre/lib/security目录下并覆盖原来默认的内容,至此加密解密的准备工作就完成了。最后,如果你正在学习SpringBoot,我想推荐这个从2016年至今连载的系列教程。点击这里,欢迎收藏转发!如果你在学习过程中遇到困难?大家可以加入我们的Spring技术交流群,参与交流讨论,更好的学习进步!欢迎关注我的:程序员DD,分享别处看不到的知识和思考