Spring 引导/Jetty + SSL:找不到密钥库(FileNotFoundException)

Spring Boot / Jetty + SSL: Keystore not found (FileNotFoundException)

我正在尝试在 Spring 引导应用程序中启用带有嵌入式码头的 SSL。

Spring 启动启动器版本:2.1.0.RELEASE

我的配置:

我使用以下命令创建了一个 keystore

keytool -genkey -keyalg RSA -alias webapp -keystore keystore.jks -storepass password -keysize 2048

生成的 keystore.jks 文件被放置在 src/main/resources/ssl.

教程大多说在 application.yaml 中引用这个密钥库应该足以 "make it work":

server:
    port: 9292
    servlet:
        context-path: /
    ssl:
        key-store: classpath:keystore.jks
        key-store-password: password
        key-alias: webapp
        key-store-type: JKS

错误:

当我尝试启动应用程序时,它失败了:

Caused by: org.springframework.boot.web.server.WebServerException: Could not find key store 'classpath:keystore.jks'
at org.springframework.boot.web.embedded.jetty.SslServerCustomizer.configureSslKeyStore(SslServerCustomizer.java:195) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.embedded.jetty.SslServerCustomizer.configureSsl(SslServerCustomizer.java:164) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.embedded.jetty.SslServerCustomizer.customize(SslServerCustomizer.java:73) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory.customizeSsl(JettyServletWebServerFactory.java:195) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory.getWebServer(JettyServletWebServerFactory.java:145) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
... 13 common frames omitted
    Caused by: java.io.FileNotFoundException: class path resource [keystore.jks] cannot be resolved to URL because it does not exist
at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:137) ~[spring-core-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.embedded.jetty.SslServerCustomizer.configureSslKeyStore(SslServerCustomizer.java:190) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
... 19 common frames omitted

当我查看 maven 目标目录时,keystore.jks 位于 classes/ssl 下,所以它应该在类路径中,对吗?

有人能指出我遗漏了什么吗?

编辑:

奇怪的是,将 keystore.jks 移动到 src/main/resources 似乎解决了这个问题。为什么 jetty 可以在那里找到密钥库,但不能在子目录 src/main/resources/ssl?

类路径默认设置为 src/main/resources。意思是如果你有 subdirectories 里面你需要指定你的 jks file:

的完整路径

key-store: classpath:ssl/keystore.jks