1

I try to configure restTemplate to call https url in spring boot 3 but that does not compile. For this i use import org.apache.http.impl.client.HttpClients import org.apache.http.ssl.SSLContexts and the code below :

fun restTemplate(): RestTemplate {
        val restTemplate = RestTemplate()

        // Load the .p12 file from the test resources directory
        val p12File = ClassPathResource("certs/clientErfin2.p12").file
        val p12Stream = FileInputStream(p12File)

        try {
            // Load the .p12 file into a KeyStore
            val keyStore = KeyStore.getInstance("PKCS12")
            keyStore.load(p12Stream, "password".toCharArray())

            // Create an SSL context with the KeyStore
            val sslContext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, "password".toCharArray())
                    .build()

            // Create an HttpClient with the SSL context
            val httpClient = HttpClients.custom()
                    .setSSLContext(sslContext)
                    .build()

            // Create a ClientHttpRequestFactory with the HttpClient
            val requestFactory: ClientHttpRequestFactory = HttpComponentsClientHttpRequestFactory(httpClient)

            // Set the ClientHttpRequestFactory on the RestTemplate
            restTemplate.requestFactory = requestFactory
        } finally {
            p12Stream.close()
        }

        return restTemplate
    }

the line val requestFactory: ClientHttpRequestFactory = HttpComponentsClientHttpRequestFactory(httpClient) does not compile because of Cannot access class 'org.apache.hc.client5.http.classic.HttpClient'. Check your module classpath for missing or conflicting dependencie. How can i fix this?

Thank's!

i tried to add the dependency hc.client5 and that does not resolve the problem

2 Answers 2

1

with combination of spring-boot 3.0.7 and httpclient5 5.2.1, did like following way,

         char[] keyStorePasswordAsCharArray = tlsConfig.keyStorePassword().toCharArray();
         char[] keyPasswordAsCharArray = tlsConfig.clientKeyPassword().toCharArray();
         char[] trustStorePasswordAsCharArray = tlsConfig.trustStorePassword().toCharArray();

         try {
            URL keyStoreUrl = ResourceUtils.getURL(tlsConfig.keyStorePath());
            URL trustStoreUrl = ResourceUtils.getURL(tlsConfig.trustStorePath());

            SSLContextBuilder SSLBuilder = SSLContexts.custom();
            SSLBuilder = SSLBuilder.loadTrustMaterial(trustStoreUrl, trustStorePasswordAsCharArray).loadKeyMaterial(keyStoreUrl,
                     keyStorePasswordAsCharArray, keyPasswordAsCharArray);

            SSLContext sslcontext = SSLBuilder.build();
            SSLConnectionSocketFactory sslConSocFactory = new SSLConnectionSocketFactory(sslcontext, new NoopHostnameVerifier());

            PoolingHttpClientConnectionManager connectionManagerBuilder = PoolingHttpClientConnectionManagerBuilder.create()
                     .setSSLSocketFactory(sslConSocFactory).build();
            
            CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManagerBuilder).build();

            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);

            restTemplate = new RestTemplate(requestFactory);
         } catch (Exception e) {
            throw new RuntimeException(e);
         }

The code should compile and work.

Sign up to request clarification or add additional context in comments.

Comments

0

I've fixed it by adding this to my gradle.build

    implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'

and importing this HttpClientBuilder:

import org.apache.hc.client5.http.impl.classic.HttpClientBuilder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.