Spring Boot 2.1:类路径资源 [keystore.p12] 无法解析为 URL,因为它不存在

Pla*_*ger 5 classpath intellij-idea kotlin spring-boot

我是 Spring Boot 和开发的新手。在 kotlin 和 gradle 中使用 Spring Boot 2。当我尝试附加 ssl 证书时出现类路径错误。Spring Boot 1.4解决方案:类路径资源 [] 无法解析为 URL,因为它不存在不起作用。

同时,我尝试复制解决方案并删除了 starter-data-jpa 的实现并使用了 starter-jdbc。但似乎没有任何效果。另一个解决方案也会引发相同的错误。

> Error starting ApplicationContext. To display the conditions report
> re-run your application with 'debug' enabled. 2019-01-16 23:20:19.511
> ERROR 9589 --- [           main] o.s.boot.SpringApplication           
> : Application run failed
> 
> org.springframework.context.ApplicationContextException: Unable to
> start web server; nested exception is
> org.springframework.boot.web.server.WebServerException: Could not load
> key store: class path resource [keystore.p12] cannot be resolved to
> URL because it does not exist     at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540)
> ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]     at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
> [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]     at
> org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
> [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
> [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
> [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]     at
> org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
> [spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]     at
> com.example.demo.DemoApplicationKt.main(DemoApplication.kt:16)
> [classes/:na] Caused by:
> org.springframework.boot.web.server.WebServerException: Could not load
> key store: class path resource [keystore.p12] cannot be resolved to
> URL because it does not exist     at
> org.springframework.boot.web.embedded.tomcat.SslConnectorCustomizer.configureSslKeyStore(SslConnectorCustomizer.java:136)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.SslConnectorCustomizer.configureSsl(SslConnectorCustomizer.java:92)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.SslConnectorCustomizer.customize(SslConnectorCustomizer.java:58)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.customizeSsl(TomcatServletWebServerFactory.java:317)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.customizeConnector(TomcatServletWebServerFactory.java:300)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:166)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:181)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    at
> org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    ... 8 common frames
> omitted Caused by: java.io.FileNotFoundException: class path resource
> [keystore.p12] cannot be resolved to URL because it does not exist    at
> org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:137)
> ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]    at
> org.springframework.boot.web.embedded.tomcat.SslConnectorCustomizer.configureSslKeyStore(SslConnectorCustomizer.java:133)
> ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]    ... 15 common frames
> omitted
Run Code Online (Sandbox Code Playgroud)

Application.property 文件

#spring.data.jpa.repositories.enabled=false



# Tell Spring Security (if used) to require requests over HTTPS
security.require-ssl=true

# The format used for the keystore
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=./keystore.p12
# The password used to generate the certificate
server.ssl.key-store-password=
# The alias mapped to the certificate
server.ssl.key-alias=tomcat
Run Code Online (Sandbox Code Playgroud)

我使用了 ./keystore 因为类路径不起作用。使用 ./ 解决它。但是当我更改 jar 位置并停止工作时。我不能将 keystore.p12 放在 build/libs/ 中与 jar 相同的位置。但我会喜欢 Kotlin 中的类路径解决方案。