From 597f6b10d4e83f4d03345fd8573b7ab59a5bec53 Mon Sep 17 00:00:00 2001 From: Mohd-Arham Date: Mon, 8 Dec 2025 10:07:21 +0530 Subject: [PATCH] add rest client --- .../bbps/config/RestClientConfig.java | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/billpaymentservice/bbps/config/RestClientConfig.java b/src/main/java/com/billpaymentservice/bbps/config/RestClientConfig.java index c248203..add99ee 100644 --- a/src/main/java/com/billpaymentservice/bbps/config/RestClientConfig.java +++ b/src/main/java/com/billpaymentservice/bbps/config/RestClientConfig.java @@ -2,38 +2,46 @@ package com.billpaymentservice.bbps.config; import java.security.cert.X509Certificate; +import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestClient; @Configuration public class RestClientConfig { - @Bean - public RestClient restClient() throws Exception { - // ✅ Create trust all SSL context - SSLContext sslContext = SSLContext.getInstance("TLS"); - TrustManager[] trustAllCerts = new TrustManager[]{ - new X509TrustManager() { - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - public void checkClientTrusted(X509Certificate[] certs, String authType) { - } - public void checkServerTrusted(X509Certificate[] certs, String authType) { - } - } - }; - sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); + @Bean + public RestClient restClient() throws Exception { - // ✅ Create RestClient with custom SSL context - return RestClient.builder() - .requestFactory(new HttpComponentsClientHttpRequestFactory()) - .build(); - } + // Create Trust-All SSL context + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } }; + + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); + + // Use JDK's Simple HTTP client with custom SSL context + SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); + requestFactory.setBufferRequestBody(false); + + // Apply SSL context to HttpsURLConnection globally + HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); + HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); + + return RestClient.builder().requestFactory(requestFactory).build(); + } } \ No newline at end of file