0

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);
    }
}
A21
  • 181
  • 3
  • 18

1 Answers1

1

Abstract class cannot be instantiated, so that's probably a reason why this code is never called. Instead of making ContextListenerExample class abstract, try to implement another method declared in ServletContextListener interface:

public void contextDestroyed(ServletContextEvent e) {
  // you can just leave it empty
}
Aleksei Budiak
  • 871
  • 5
  • 16
  • This did allow me to reach the code contained within contextIntialized after implementing contextDestroyed, but it doesn't seem to be registering the trustStore. – A21 Aug 11 '17 at 20:26
  • Well, that's another question. Probably, at the moment you specify these properties Spring already has the keystore/truststore initialized. You can try different approach described [here](https://stackoverflow.com/a/44849648/7337255), for example. – Aleksei Budiak Aug 11 '17 at 20:32
  • No, I attempted to set the trustStore and keyStore as Resource elements as provided in the answer page you linked me too, but no luck creating the 'RestTemplate' bean. I've edited my original question to incorporate the new class that I've added to my project. – A21 Aug 16 '17 at 21:07