I'm trying to set the system properties for a trustStore and keyStore as follows:
@WebListener
public abstract class ContextListenerExample implements ServletContextListener {
public void contextInitialized(ServletContextEvent e){
System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\trustCert.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "test123");
System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStore", "C:\\Users\\keyCert.p12");
System.setProperty("javax.net.ssl.keyStorePassword", "keystore");
System.setProperty("javax.net.ssl.keyStoreType", "keystoreType");
}
}
I followed the example here but when I run my application, it never reaches the contextIntialized method. In addition, I had to change the ContextListenerExample class to be abstract. Is there another way of setting system properties, or am I missing some other files that need to be modified?
I've added a new file SslConfiguration class:
@Configuration
public class SslConfiguration {
@Value("${C:\\Users\\A21\\src\\main\\java\\org\\test\\certificates\\test.jks}")
private Resource trustStore;
@Value("test123")
private String trustStorePassword;
@Value("${C:\\Users\\A21\\src\\main\\java\\org\\test\\certificates\\test.p12}")
private Resource keyStore;
@Value("teststore")
private String keyStorePassword;
@Bean
RestTemplate restTemplate() throws Exception {
SSLContext sslContext = new SSLContextBuilder()
.loadKeyMaterial(
keyStore.getFile(),
keyStorePassword.toCharArray(),
keyStorePassword.toCharArray())
.loadTrustMaterial(
trustStore.getURL(),
trustStorePassword.toCharArray(),
// use this for self-signed certificates only:
new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory socketFactory =
new SSLConnectionSocketFactory(sslContext);
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory(httpClient);
return new RestTemplate(factory);
}
}