某天运行项目时出现:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.security.KeyPair]: Factory method 'keyPair' threw exception; nested exception is java.lang.IllegalStateException: Cannot load keys from store: class path resource [hmall.jks]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:652) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
... 48 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot load keys from store: class path resource [hmall.jks]
at org.springframework.security.rsa.crypto.KeyStoreKeyFactory.getKeyPair(KeyStoreKeyFactory.java:93) ~[spring-security-rsa-1.0.9.RELEASE.jar:na]
at com.gjs.springcloudgateway.config.SecurityConfig.keyPair(SecurityConfig.java:29) ~[classes/:na]
at com.gjs.springcloudgateway.config.SecurityConfig$$EnhancerBySpringCGLIB$$44ba8e8e.CGLIB$keyPair$0(<generated>) ~[classes/:na]
at com.gjs.springcloudgateway.config.SecurityConfig$$EnhancerBySpringCGLIB$$44ba8e8e$$FastClassBySpringCGLIB$$dbc4206b.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
at com.gjs.springcloudgateway.config.SecurityConfig$$EnhancerBySpringCGLIB$$44ba8e8e.keyPair(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
... 49 common frames omitted
Caused by: java.io.IOException: Invalid keystore format
at sun.security.provider.JavaKeyStore.engineLoad(Unknown Source) ~[na:1.8.0_101]
at sun.security.provider.JavaKeyStore$JKS.engineLoad(Unknown Source) ~[na:1.8.0_101]
at sun.security.provider.KeyStoreDelegator.engineLoad(Unknown Source) ~[na:1.8.0_101]
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(Unknown Source) ~[na:1.8.0_101]
at java.security.KeyStore.load(Unknown Source) ~[na:1.8.0_101]
at org.springframework.security.rsa.crypto.KeyStoreKeyFactory.getKeyPair(KeyStoreKeyFactory.java:70) ~[spring-security-rsa-1.0.9.RELEASE.jar:na]
... 60 common frames omitted
打开jks文件才发现密钥文件过期了
解决方法
打开cmd,重新生成jks文件,注意需要配置了jdk环境才能正常执行。
keytool -genkeypair -alias mydomain -keyalg RSA -keysize 2048 -validity 365 -keystore your_keystore.jks
其中:your_keystore.jks
是你要生成的 .jks 文件的路径和文件名。根据需要,可以修改别名(alias)、密钥算法(keyalg)、密钥长度(keysize)和有效期(validity)等参数。
执行上述命令后,keytool 会提示你输入密钥库的密码和其他信息,如名称、组织单位、城市、省份、国家代码等。按照提示输入相应信息即可生成新的 .jks 文件。
生成的文件会在你打开cmd的地方。将jks放到项目中
重新配置appplication文件
gjs:
jwt:
location: classpath:gjs.jks # 秘钥地址
alias: gjs # 秘钥别名
password: gjs123 # 秘钥文件密码
tokenTTL: 30m # 登录有效期
auth:
excludePaths: # 无需登录校验的路径
- /**/v2/api-docs
重新启动项目完美启动