老师在做redis的过程中在Service测试的时候,出现了错误。
来源:11-6 引入缓存技术之编码实现下

慕码人7211748
2019-02-24
二月 24, 2019 12:07:49 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
二月 24, 2019 12:07:49 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@3567135c, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@327471b5, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4157f54e, org.springframework.test.context.support.DirtiesContextTestExecutionListener@90f6bfd, org.springframework.test.context.transaction.TransactionalTestExecutionListener@47f6473, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@15975490]
二月 24, 2019 12:07:49 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/spring-dao.xml]
二月 24, 2019 12:07:49 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/spring-service.xml]
二月 24, 2019 12:07:49 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring/spring-redis.xml]
二月 24, 2019 12:07:49 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@28feb3fa: startup date [Sun Feb 24 00:07:49 GMT+08:00 2019]; root of context hierarchy
二月 24, 2019 12:07:50 上午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4157f54e] to prepare test instance [com.o2o.dao.AreaDaoTest@6a2b953e]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.RuntimeException: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.o2o.util.DESUtil.getDecryptString(DESUtil.java:92)
at com.o2o.util.EncryptPropertyPlaceholderConfigurer.convertProperty(EncryptPropertyPlaceholderConfigurer.java:16)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.convertProperties(PropertyResourceConfigurer.java:106)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:83)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:281)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:161)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
… 25 more
Caused by: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:913)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314)
at javax.crypto.Cipher.doFinal(Cipher.java:2121)
at com.o2o.util.DESUtil.getDecryptString(DESUtil.java:87)
… 38 more
我看是解密过程出了错?老师有什么解决方案吗?还是其他问题,麻烦老师了
1回答
-
翔仔
2019-02-24
同学好,导致这个caused by的错误的原因比较多,没办法精确定位,同学可以按照我的相关代码来,直接拷贝复制替换掉同学的,然后试试,如果okay了,再对比下异同
DESUtil.java
package com.imooc.o2o.util; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。 * * @author xiangze * */ public class DESUtil { private static Key key; // 设置密钥key private static String KEY_STR = "myKey"; private static String CHARSETNAME = "UTF-8"; private static String ALGORITHM = "DES"; static { try { // 生成DES算法对象 KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM); // 运用SHA1安全策略 SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // 设置上密钥种子 secureRandom.setSeed(KEY_STR.getBytes()); // 初始化基于SHA1的算法对象 generator.init(secureRandom); // 生成密钥对象 key = generator.generateKey(); generator = null; } catch (Exception e) { throw new RuntimeException(e); } } /** * 获取加密后的信息 * * @param str * @return */ public static String getEncryptString(String str) { // 基于BASE64编码,接收byte[]并转换成String BASE64Encoder base64encoder = new BASE64Encoder(); try { // 按UTF8编码 byte[] bytes = str.getBytes(CHARSETNAME); // 获取加密对象 Cipher cipher = Cipher.getInstance(ALGORITHM); // 初始化密码信息 cipher.init(Cipher.ENCRYPT_MODE, key); // 加密 byte[] doFinal = cipher.doFinal(bytes); // byte[]to encode好的String并返回 return base64encoder.encode(doFinal); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } } /** * 获取解密之后的信息 * * @param str * @return */ public static String getDecryptString(String str) { // 基于BASE64编码,接收byte[]并转换成String BASE64Decoder base64decoder = new BASE64Decoder(); try { // 将字符串decode成byte[] byte[] bytes = base64decoder.decodeBuffer(str); // 获取解密对象 Cipher cipher = Cipher.getInstance(ALGORITHM); // 初始化解密信息 cipher.init(Cipher.DECRYPT_MODE, key); // 解密 byte[] doFinal = cipher.doFinal(bytes); // 返回解密之后的信息 return new String(doFinal, CHARSETNAME); } catch (Exception e) { // TODO: handle exception throw new RuntimeException(e); } } public static void main(String[] args) { System.out.println(getEncryptString("work")); } }
如果有问题继续在这里提:)
00
相似问题