window.ENTITIES={'/api/snippets/java/spring%20boot%203%20-%20resttemplate%20with%20custom%20https%2fssl%20certificate':[{"result":true,"message":null,"batch":{"type":"java","name":"spring boot 3 - resttemplate with custom https/ssl certificate","items":[{"id":"1GM8Yp","type":"java","name":"Spring Boot 3 - RestTemplate with custom HTTPS/SSL certificate","content":"// ---------------------------------------------------------------------\n// pom.xml file:\n// ---------------------------------------------------------------------\n\n/*\n \n org.apache.httpcomponents.client5\n httpclient5\n 5.2.1\n \n*/\n\n\n\n// ---------------------------------------------------------------------\n// Program.java file:\n// ---------------------------------------------------------------------\n\npackage com.example;\n\nimport java.io.File;\nimport java.io.IOException;\nimport java.security.KeyManagementException;\nimport java.security.KeyStoreException;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.cert.CertificateException;\n\nimport javax.net.ssl.SSLContext;\n\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.http.client.ClientHttpRequestFactory;\nimport org.springframework.http.client.HttpComponentsClientHttpRequestFactory;\nimport org.springframework.web.client.RestTemplate;\nimport org.apache.hc.client5.http.classic.HttpClient;\nimport org.apache.hc.client5.http.impl.classic.HttpClientBuilder;\nimport org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;\nimport org.apache.hc.client5.http.socket.ConnectionSocketFactory;\nimport org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;\nimport org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;\nimport org.apache.hc.core5.http.URIScheme;\nimport org.apache.hc.core5.http.config.Registry;\nimport org.apache.hc.core5.http.config.RegistryBuilder;\nimport org.apache.hc.core5.ssl.SSLContextBuilder;\n\npublic class Program {\n\n\tpublic static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {\n\n\t\tString keystorePath = \"keystore.p12\"; // must be the same like used by https://localhost:5000\n\t\tString keystorePassword = \"Pa$$word\";\n\n\t\tFile keystoreFile = new File(keystorePath);\n\n\t\tSSLContext sslContext = SSLContextBuilder.create()\n\t\t\t.loadTrustMaterial(keystoreFile, keystorePassword.toCharArray())\n\t\t\t.build();\n\n\t\tRegistry socketRegistry = RegistryBuilder.create()\n\t\t\t.register(URIScheme.HTTPS.getId(), new SSLConnectionSocketFactory(sslContext))\n\t\t\t.register(URIScheme.HTTP.getId(), new PlainConnectionSocketFactory())\n\t\t\t.build();\n\n\t\tHttpClient httpClient = HttpClientBuilder.create()\n\t\t\t.setConnectionManager(new PoolingHttpClientConnectionManager(socketRegistry))\n\t\t\t.setConnectionManagerShared(true)\n\t\t\t.build();\n\n ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);\n RestTemplate restTemplate = new RestTemplate(requestFactory);\n\n\t\tString responseBody = restTemplate.getForObject(\"https://localhost:5000/path/to/resource\", String.class);\n\n\t\tSystem.out.println(responseBody);\n\t}\n}\n\n\n// See also:\n//\n// 1. https://dirask.com/snippets/Spring-Boot-3-RestTemplate-with-ignored-HTTPS-SSL-certificate-ignore-untrusted-certificates-1bQZJj","source":"","author":{"id":"4a6xA0","name":"Broncono","avatar":"1629030394463__4a6xA0__w40px_h40px.png","points":466,"role":"BASIC"},"creationTime":1674959316000,"updateTime":1675813504000,"removalTime":null}]}}]};