1
1
package com .tennis .config ;
2
2
3
- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
4
- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
5
- import org .apache .http .impl .client .CloseableHttpClient ;
6
- import org .apache .http .impl .client .HttpClients ;
7
- import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
8
- import org .apache .http .client .config .RequestConfig ;
9
- import org .apache .http .ssl .TrustStrategy ;
10
3
import org .springframework .context .annotation .Bean ;
11
4
import org .springframework .context .annotation .Configuration ;
12
- import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
5
+ import org .springframework .http .client .SimpleClientHttpRequestFactory ;
13
6
import org .springframework .web .client .RestTemplate ;
14
7
15
- import javax .net .ssl .SSLContext ;
8
+ import javax .net .ssl .*;
9
+ import java .security .KeyManagementException ;
10
+ import java .security .NoSuchAlgorithmException ;
16
11
import java .security .cert .X509Certificate ;
17
12
18
13
/**
22
17
public class RestTemplateConfig {
23
18
24
19
@ Bean
25
- public RestTemplate restTemplate () throws Exception {
26
- // Create trust strategy that trusts all certificates
27
- TrustStrategy acceptingTrustStrategy = (X509Certificate [] chain , String authType ) -> true ;
28
-
29
- // Create SSL context with trust all strategy
30
- SSLContext sslContext = org .apache .http .ssl .SSLContexts .custom ()
31
- .loadTrustMaterial (null , acceptingTrustStrategy )
32
- .build ();
33
-
34
- // Create SSL connection socket factory
35
- SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory (
36
- sslContext ,
37
- new String [] { "TLSv1.2" },
38
- null ,
39
- NoopHostnameVerifier .INSTANCE );
40
-
41
- // Create connection manager with pooling
42
- PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager ();
43
- connectionManager .setMaxTotal (100 );
44
- connectionManager .setDefaultMaxPerRoute (20 );
45
-
46
- // Create request config with timeouts
47
- RequestConfig requestConfig = RequestConfig .custom ()
48
- .setConnectTimeout (10000 ) // 10 seconds
49
- .setSocketTimeout (30000 ) // 30 seconds
50
- .build ();
20
+ public RestTemplate restTemplate () throws NoSuchAlgorithmException , KeyManagementException {
21
+ // Create a trust manager that trusts all certificates
22
+ TrustManager [] trustAllCerts = new TrustManager [] {
23
+ new X509TrustManager () {
24
+ public X509Certificate [] getAcceptedIssuers () {
25
+ return new X509Certificate [0 ];
26
+ }
27
+ public void checkClientTrusted (X509Certificate [] certs , String authType ) {
28
+ }
29
+ public void checkServerTrusted (X509Certificate [] certs , String authType ) {
30
+ }
31
+ }
32
+ };
33
+
34
+ // Create SSL context that trusts all certificates
35
+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
36
+ sslContext .init (null , trustAllCerts , new java .security .SecureRandom ());
37
+
38
+ // Create SSL hostname verifier that accepts all hostnames
39
+ HostnameVerifier allHostsValid = (hostname , session ) -> true ;
51
40
52
- // Create HTTP client with SSL configuration and timeouts
53
- CloseableHttpClient httpClient = HttpClients .custom ()
54
- .setSSLSocketFactory (csf )
55
- .setConnectionManager (connectionManager )
56
- .setDefaultRequestConfig (requestConfig )
57
- .build ();
41
+ // Set default SSL context and hostname verifier
42
+ HttpsURLConnection .setDefaultSSLSocketFactory (sslContext .getSocketFactory ());
43
+ HttpsURLConnection .setDefaultHostnameVerifier (allHostsValid );
58
44
59
- // Create request factory with custom HTTP client
60
- HttpComponentsClientHttpRequestFactory requestFactory =
61
- new HttpComponentsClientHttpRequestFactory (httpClient );
45
+ // Create simple request factory with timeouts
46
+ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory ();
47
+ factory .setConnectTimeout (10000 ); // 10 seconds
48
+ factory .setReadTimeout (30000 ); // 30 seconds
62
49
63
- return new RestTemplate (requestFactory );
50
+ return new RestTemplate (factory );
64
51
}
65
52
}
0 commit comments