Spring Boot 2.1: Classpath resource [keystore.p12] cant be resolved to URL because it doesn't exist

Viewed 8326

I am new to spring boot and development in all. Using Spring Boot 2 with kotlin and gradle. The classpath error comes when I try attaching ssl certificate. Solution on Spring Boot 1.4 : class path resource [] cannot be resolved to URL because it does not exist doesn't work.

Meanwhile, I tried replicating the solution and removed implementation of starter-data-jpa and used starter-jdbc. But nothing seems to works. The other solution also throws same error.

> 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

Application.property file

#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

I have used ./keystore as classpath is not working. Using ./ solves it. But when I make jar location changes and it stops working. I cant put keystore.p12 at same location as of jar in build/libs/. But I would love a classpath solution in Kotlin.

0 Answers
Related