问题描述:
将项目从测试环境升级到生产环境后,启动项目报出了以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wepaySupport': Invocation of init method failed; nested exception is java.lang.RuntimeException: org.wepay.exception.WepayException: java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118) [catalina.jar:7.0.73] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634) [catalina.jar:7.0.73] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) [catalina.jar:7.0.73] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571) [catalina.jar:7.0.73] at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561) [catalina.jar:7.0.73] at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_79] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_79] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_79] at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79] Caused by: java.lang.RuntimeException: org.wepay.exception.WepayException: java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded at org.skyopen.web.jianpin.pay.wepay.WepaySupport.initWepay(WepaySupport.java:55) ~[skyopen-jianpin-web-2.0.0.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79] at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 23 more Caused by: org.wepay.exception.WepayException: java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded at org.wepay.core.Wepay.initSSLSocketFactory(Wepay.java:152) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.Wepay.init(Wepay.java:131) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.WepayBuilder.build(WepayBuilder.java:53) ~[skyopen-common-2.0.0.jar:?] at org.skyopen.web.jianpin.pay.wepay.WepaySupport.initWepay(WepaySupport.java:50) ~[skyopen-jianpin-web-2.0.0.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79] at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 23 more Caused by: java.io.IOException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1317) ~[?:1.7.0_79] at java.security.KeyStore.load(KeyStore.java:1214) ~[?:1.7.0_79] at org.wepay.core.Wepay.initSSLSocketFactory(Wepay.java:142) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.Wepay.init(Wepay.java:131) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.WepayBuilder.build(WepayBuilder.java:53) ~[skyopen-common-2.0.0.jar:?] at org.skyopen.web.jianpin.pay.wepay.WepaySupport.initWepay(WepaySupport.java:50) ~[skyopen-jianpin-web-2.0.0.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79] at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 23 more Caused by: javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) ~[sunjce_provider.jar:1.7.0_85] at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) ~[sunjce_provider.jar:1.7.0_85] at com.sun.crypto.provider.PKCS12PBECipherCore.implDoFinal(PKCS12PBECipherCore.java:355) ~[sunjce_provider.jar:1.7.0_85] at com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndRC2_40.engineDoFinal(PKCS12PBECipherCore.java:462) ~[sunjce_provider.jar:1.7.0_85] at javax.crypto.Cipher.doFinal(Cipher.java:2087) ~[?:1.7.0_71] at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1308) ~[?:1.7.0_79] at java.security.KeyStore.load(KeyStore.java:1214) ~[?:1.7.0_79] at org.wepay.core.Wepay.initSSLSocketFactory(Wepay.java:142) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.Wepay.init(Wepay.java:131) ~[skyopen-common-2.0.0.jar:?] at org.wepay.core.WepayBuilder.build(WepayBuilder.java:53) ~[skyopen-common-2.0.0.jar:?] at org.skyopen.web.jianpin.pay.wepay.WepaySupport.initWepay(WepaySupport.java:50) ~[skyopen-jianpin-web-2.0.0.jar:?] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79] at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE] ... 23 more
问题原因:
经过核对比较,最终发现是证书原因导致的。一个商户号(mchId),对应一个API密钥(appKey),对应着一个证书文件(apiclient_cert.p12),升级后,商户号和API密钥被更改了,但是证书文件却还是测试环境的证书,结果导致了证书和密钥不一致的问题。
解决方案:
将测试环境的证书替换成生产环境的证书,再次部署启动web服务器,问题解决。
附录:
API密钥如何设置:
1. 登录微信支付商户平台: pay.weixin.qq.com
2. 在 账户中心 -> API安全中,设置API密钥
如何下载证书:
1. 设置完密钥后,就可以下载证书到本地进行使用了
2. 下载的文件是cert.zip,项目中使用的多是apiclient_cert.p12证书文件