Skip to content

Instantly share code, notes, and snippets.

@MdGolam-Kibria
Created August 22, 2024 06:42
Show Gist options
  • Select an option

  • Save MdGolam-Kibria/c8fdb1c8896d0742b133a0a388baa0ca to your computer and use it in GitHub Desktop.

Select an option

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.
@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;
}
@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