[Edit]
+
0
-
0
Spring Boot 3 - RestTemplate with ignored HTTPS/SSL certificate (ignore untrusted certificates)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79// --------------------------------------------------------------------- // pom.xml file: // --------------------------------------------------------------------- /* <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.2.1</version> </dependency> */ // --------------------------------------------------------------------- // Program.java file: // --------------------------------------------------------------------- package com.example; import java.io.IOException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import javax.net.ssl.SSLContext; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import org.apache.hc.client5.http.classic.HttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.client5.http.socket.ConnectionSocketFactory; import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.core5.http.URIScheme; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.ssl.SSLContextBuilder; public class Program { public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { SSLContext sslContext = SSLContextBuilder.create() .loadTrustMaterial((X509Certificate[] certificateChain, String authType) -> true) // <--- accepts each certificate .build(); Registry<ConnectionSocketFactory> socketRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register(URIScheme.HTTPS.getId(), new SSLConnectionSocketFactory(sslContext)) .register(URIScheme.HTTP.getId(), new PlainConnectionSocketFactory()) .build(); HttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(new PoolingHttpClientConnectionManager(socketRegistry)) .setConnectionManagerShared(true) .build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(requestFactory); String responseBody = restTemplate.getForObject("https://localhost:5000/path/to/resource", String.class); System.out.println(responseBody); } } // See also: // // 1. https://dirask.com/snippets/Spring-Boot-3-RestTemplate-with-custom-HTTPS-SSL-certificate-1GM8Yp // References: // // 1. https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5