add rest client
This commit is contained in:
@@ -2,38 +2,46 @@ package com.billpaymentservice.bbps.config;
|
|||||||
|
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.TrustManager;
|
||||||
import javax.net.ssl.X509TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class RestClientConfig {
|
public class RestClientConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RestClient restClient() throws Exception {
|
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());
|
|
||||||
|
|
||||||
// ✅ Create RestClient with custom SSL context
|
// Create Trust-All SSL context
|
||||||
return RestClient.builder()
|
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
|
||||||
.requestFactory(new HttpComponentsClientHttpRequestFactory())
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
.build();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user