启动加密报错

214 阅读6分钟

启动报错

背景:一个微服务项目中cofig-server启动时报错,起初以为是端口被占用,但是排查后一直没有找到占用的端口,后来才发现加密问题。

报错信息

2022-09-14 09:43:18.073  INFO 3292 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@56673b2c: startup date [Wed Sep 14 09:43:18 CST 2022]; root of context hierarchy
2022-09-14 09:43:18.262  INFO 3292 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2022-09-14 09:43:18.300  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$62b337f0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

2022-09-14 09:43:19.528  INFO 3292 --- [           main] c.y.base.service.config.ConfigServerApp  : The following profiles are active: native
2022-09-14 09:43:19.540  INFO 3292 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2f7dcef2: startup date [Wed Sep 14 09:43:19 CST 2022]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@56673b2c
2022-09-14 09:43:20.485  INFO 3292 --- [           main] o.s.i.config.IntegrationRegistrar        : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2022-09-14 09:43:20.888  INFO 3292 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=2f696c4b-b192-342c-8a20-ff2b6d76d4aa
2022-09-14 09:43:20.905  INFO 3292 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2022-09-14 09:43:20.908  INFO 3292 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2022-09-14 09:43:20.917  INFO 3292 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2022-09-14 09:43:20.947  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.ydy.base.service.config.feign.IAliproFeign' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:20.948  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.ydy.base.service.config.feign.IUsercenterFeign' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:20.972  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$d04f321] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:21.190  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration' of type [org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration$$EnhancerBySpringCGLIB$$78c5db34] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:21.229  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [org.springframework.beans.factory.config.PropertiesFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:21.234  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationGlobalProperties' of type [java.util.Properties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:21.278  INFO 3292 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$62b337f0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-09-14 09:43:21.653  INFO 3292 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8887 (http)
2022-09-14 09:43:21.660  INFO 3292 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-14 09:43:21.661  INFO 3292 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2022-09-14 09:43:21.764  INFO 3292 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-09-14 09:43:21.764  INFO 3292 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2224 ms
2022-09-14 09:43:22.082  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2022-09-14 09:43:22.086  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2022-09-14 09:43:22.087  INFO 3292 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2022-09-14 09:43:22.225  INFO 3292 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7283877: startup date [Wed Sep 14 09:43:22 CST 2022]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2f7dcef2
2022-09-14 09:43:22.339  INFO 3292 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2022-09-14 09:43:23.015  INFO 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 此时容器中的配置项状态为: endpoint:Ready;
2022-09-14 09:43:23.015  INFO 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 此时容器中的配置项状态为: project:Ready;endpoint:Ready;
2022-09-14 09:43:23.015  INFO 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 此时容器中的配置项状态为: project:Ready;endpoint:Ready;accessKey:Ready;
2022-09-14 09:43:23.015  INFO 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 此时容器中的配置项状态为: project:Ready;endpoint:Ready;secretKey:Ready;accessKey:Ready;
2022-09-14 09:43:23.015  INFO 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 此时容器中的配置项状态为: project:Ready;endpoint:Ready;secretKey:Ready;accessKey:Ready;logstore:Ready;
2022-09-14 09:43:23.016  WARN 3292 --- [           main] c.y.f.log.manager.LogClientCfgRfsMgr     : 重新初始化clog配置
2022-09-14 09:43:23.820 ERROR 3292 --- [           main] o.s.b.b.PropertiesConfigurationFactory   : Properties configuration failed validation
2022-09-14 09:43:23.821 ERROR 3292 --- [           main] o.s.b.b.PropertiesConfigurationFactory   : Field error in object 'clog' on field 'logstore': rejected value [dev_action_exception_store]; codes [methodInvocation.clog.logstore,methodInvocation.logstore,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clog.logstore,logstore]; arguments []; default message [logstore]]; default message [Property 'logstore' threw exception; nested exception is java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size or default parameters]
2022-09-14 09:43:23.822  WARN 3292 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configInitController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initStatusServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ydy.frame.log.config.AliyunLogClientConfig': Could not bind properties to AliyunLogClientConfig (prefix=clog, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'clog' on field 'logstore': rejected value [dev_action_exception_store]; codes [methodInvocation.clog.logstore,methodInvocation.logstore,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [clog.logstore,logstore]; arguments []; default message [logstore]]; default message [Property 'logstore' threw exception; nested exception is java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size or default parameters]
2022-09-14 09:43:23.823  INFO 3292 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7283877: startup date [Wed Sep 14 09:43:22 CST 2022]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2f7dcef2
2022-09-14 09:43:23.828  WARN 3292 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception thrown from ApplicationListener handling ContextClosedEvent

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'rabbitConnectionFactory': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:235) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:192) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:128) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:399) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:991) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:958) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.cloud.context.named.NamedContextFactory.destroy(NamedContextFactory.java:76) [spring-cloud-context-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:272) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
	at com.ydy.base.service.config.ConfigServerApp.main(ConfigServerApp.java:35) [classes/:na]

2022-09-14 09:43:23.829  INFO 3292 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-09-14 09:43:23.852  INFO 3292 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2022-09-14 09:43:23.858 ERROR 3292 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target com.ydy.frame.log.config.AliyunLogClientConfig@3cd89c72 failed:

    Property: clog.logstore
    Value: dev_action_exception_store
    Reason: Property 'logstore' threw exception; nested exception is java.lang.RuntimeException: java.security.InvalidKeyException: Illegal key size or default parameters


Action:

Update your application's configuration

Disconnected from the target VM, address: '127.0.0.1:53642', transport: 'socket'

Process finished with exit code 1

解决过程

网上搜索发现,由于key的变更(由原来的128位变更为256位),导致加密/解密的失败。

之所以会出现这种问题,我们就要了解一个新的东西——JCE。在Java的核心类库中有一个JCE(Java Cryptography Extension),JCE是一组包,它们提供用于加密、密钥生成和协商以及 Message Authentication Code(MAC)算法的框架和实现,所以这个是实现加密解密的重要类库。

之所以会出现上述的报错问题:是因为AES在进行加密处理的时候并没有经过特殊的处理,但是在使用256位密钥加解密的时候,如果不进行特殊处理的话,往往会出现报“java.security.InvalidKeyException: Illegal key size or default parameters”的异常。

通过上面描述,我们知道了问题的所在,下面开始解决。我们要做的就是更换JCE的库,该文件位置在你的JDK/jre/lib/security 目录下面,该目录下我们可以看到两个jar包:local_policy.jarUS_export_policy.jar,这两个jar包是jdk自带的。我们需要下载支持256位密钥加密的jar包。

image.png

备注:一定要下载与自己JDK版本一致的JCE,否则无效。 JDK8: 其对应的JCE下载地址为:www.oracle.com/technetwork…

下载完后,解压,将其中的“local_policy.jar ”和“US_export_policy.jar”两个文件替换掉自己%JAVE_HOME%\jre\lib\security文件夹下对应的原文件(%JAVE_HOME%是自己电脑的Java路径)。

JDK7: 其对应的JCE下载地址为:www.oracle.com/technetwork…

下载完后,解压,将其中的“local_policy.jar ”和“US_export_policy.jar”两个文件替换掉自己%JAVE_HOME%\jre\lib\security文件夹下对应的原文件(%JAVE_HOME%是自己电脑的Java路径)。

JDK6: 其对应的JCE下载地址为:www.oracle.com/technetwork…

下载完后,解压,将其中的“local_policy.jar ”和“US_export_policy.jar”两个文件替换掉自己%JAVE_HOME%\jre\lib\security文件夹下对应的原文件(%JAVE_HOME%是自己电脑的Java路径)。

JDK5: 其对应的JCE下载地址为:download.csdn.net/detail/scs2…

下载完后,解压,将其中的“local_policy.jar ”和“US_export_policy.jar”两个文件替换掉自己%JAVE_HOME%\jre\lib\security文件夹下对应的原文件(%JAVE_HOME%是自己电脑的Java路径)。

image.png

参考地址:blog.csdn.net/tomatocc/ar…