Created
August 22, 2024 06:42
-
-
Save MdGolam-Kibria/c8fdb1c8896d0742b133a0a388baa0ca to your computer and use it in GitHub Desktop.
RestTemplate bean for ignore SSL Handshake and call using and without proxy server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Bean("sslHandshakeIgnoreRestTemplate") | |
| public RestTemplate sslHandshakeIgnoreRestTemplate() { | |
| // Create a TrustManager that accepts all certificates | |
| TrustManager[] trustAllCertificates = new TrustManager[] { | |
| new X509TrustManager() { | |
| public X509Certificate[] getAcceptedIssuers() { | |
| return null; | |
| } | |
| public void checkClientTrusted(X509Certificate[] certs, String authType) {} | |
| public void checkServerTrusted(X509Certificate[] certs, String authType) {} | |
| } | |
| }; | |
| try { | |
| // Set up a SSL context that ignores all certificate validation | |
| SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| sslContext.init(null, trustAllCertificates, new java.security.SecureRandom()); | |
| HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | |
| // Set up a hostname verifier that accepts all hostnames | |
| HostnameVerifier allHostsValid = (hostname, session) -> true; | |
| HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| // Create a RestTemplate with a SimpleClientHttpRequestFactory | |
| // This factory allows disabling SSL certificate validation | |
| RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory()); | |
| return restTemplate; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Bean("sslHandshakeIgnoreRestTemplate") | |
| public RestTemplate sslHandshakeIgnoreRestTemplate() { | |
| // Create a TrustManager that accepts all certificates | |
| TrustManager[] trustAllCertificates = new TrustManager[] { | |
| new X509TrustManager() { | |
| public X509Certificate[] getAcceptedIssuers() { | |
| return null; | |
| } | |
| public void checkClientTrusted(X509Certificate[] certs, String authType) {} | |
| public void checkServerTrusted(X509Certificate[] certs, String authType) {} | |
| } | |
| }; | |
| try { | |
| // Set up a SSL context that ignores all certificate validation | |
| SSLContext sslContext = SSLContext.getInstance("SSL"); | |
| sslContext.init(null, trustAllCertificates, new java.security.SecureRandom()); | |
| HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); | |
| // Set up a hostname verifier that accepts all hostnames | |
| HostnameVerifier allHostsValid = (hostname, session) -> true; | |
| HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| // Create a RestTemplate with a proxy server as gateway | |
| SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); | |
| Proxy proxy =new Proxy(Proxy.Type.HTTP,new InetSocketAddress("172.25.4.170",8080)); | |
| // This factory allows disabling SSL certificate validation | |
| requestFactory.setProxy(proxy); | |
| RestTemplate restTemplate = new RestTemplate(requestFactory); | |
| return restTemplate; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment