'How to solve "java.io.FileNotFoundException: class path resource cannot be resolved to URL" when trying to add ssl certificate?
I am trying to add an existing SSL certificate(.cer) to a Spring boot application. Followed the following steps,
Step 1: Converted the existing .cer certificate to .p12 certificate using the below command in keytool of JDK 8.
keytool -import -alias cert -file myCertificate.crt -keystore certificate.p12 -storepass password
Step 2 : Placed this certificate.p12 that was generated in "resources" folder of the application.
Step 3 : Added the below configuration in "application.properties"
server.ssl.key-store = classpath:certificate.p12
server.ssl.key-store-password=password
server.ssl.key-store-type=pkcs12
server.ssl.key-alias=cert
server.ssl.key-password=password
server.ssl.enabled=true
server.port=8443
security.require-ssl=true
Step 4 : Added the below "SecurityConfig.java" file in application.
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requiresChannel().anyRequest().requiresSecure();
}
}
Step 5 : Added the below code in "Application.java"
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
private Connector redirectConnector() {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
After adding all the above code and configuration changes. I am getting the below exception when I start the spring boot application - Caused by: org.springframework.boot.web.server.WebServerException: Could not load key store 'classpath:certificate.p12' Caused by: java.io.FileNotFoundException: class path resource [certificate.p12] cannot be resolved to URL because it does not exist. Kindly help to fix this issue. Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
