我的Spring Boot rest应用程序中的 src / main / resources 下有两个jks文件
我正在设置资源文件 test1.jks 的路径,在Spring Boot入口点使用相对如下的相关工作正常。这些jks文件用于调用https服务。
public class TestApp {
static
{
System.setProperty("javax.net.ssl.trustStore", "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
System.setProperty("javax.net.ssl.trustStorePassword", password);
System.setProperty("javax.net.ssl.keyStore", "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
System.setProperty("javax.net.ssl.keyStorePassword", password);
}
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
application.properties
server.port=8443
server.ssl.key-store=test2.jks
server.ssl.key-store-password=test
server.ssl.trust-store=classpath:test2.jks
server.ssl.trust-store-password=test
server.ssl.key-password=test
当我使用mvn spring-boot:run
运行时,一切正常,但是当我将其作为可执行jar java -jar target/xxx-xxx-service-0.1.1-SNAPSHOT.jar
运行时,我收到以下错误。
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Could not find key store classpath:test2.jks
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:292)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSsl(TomcatEmbeddedServletContainerFactory.java:271)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.customizeConnector(TomcatEmbeddedServletContainerFactory.java:248)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:147)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
... 13 more
Caused by: java.io.FileNotFoundException: class path resource [test2.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/xxx/xxx/Himalay/xxx/xxx-xxx-service/target/xxx-xxx-service-0.1.17-SNAPSHOT.jar!/test2.jks
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:212)
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:175)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:288)
... 18 more
在评论 application.properties 之后,我发现在使用可执行jar命令运行时它甚至无法读取 test1.jks ,但在 mvn spring-boot下正常运行:运行,如何使用executable jar命令运行它?
注意:如果我使用jks文件的绝对路径,它可以正常使用这两个命令。
答案 0 :(得分:0)
尝试一下:
String filePath= Thread.currentThread().getContextClassLoader().getResource("your-trust-store.jks").getFile();
System.setProperty("javax.net.ssl.keyStore", filepath);