Skip to content

Commit dd03784

Browse files
committed
Fix SSL certificate issues with simplified RestTemplate configuration
1 parent b019b40 commit dd03784

File tree

5 files changed

+34
-52
lines changed

5 files changed

+34
-52
lines changed

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@
8484
<artifactId>spring-boot-starter-test</artifactId>
8585
<scope>test</scope>
8686
</dependency>
87-
88-
<!-- Apache HttpClient for better SSL handling -->
89-
<dependency>
90-
<groupId>org.apache.httpcomponents</groupId>
91-
<artifactId>httpclient</artifactId>
92-
<version>4.5.13</version>
93-
</dependency>
9487
</dependencies>
9588

9689
<build>
Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com.tennis.config;
22

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;
103
import org.springframework.context.annotation.Bean;
114
import org.springframework.context.annotation.Configuration;
12-
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
5+
import org.springframework.http.client.SimpleClientHttpRequestFactory;
136
import org.springframework.web.client.RestTemplate;
147

15-
import javax.net.ssl.SSLContext;
8+
import javax.net.ssl.*;
9+
import java.security.KeyManagementException;
10+
import java.security.NoSuchAlgorithmException;
1611
import java.security.cert.X509Certificate;
1712

1813
/**
@@ -22,44 +17,36 @@
2217
public class RestTemplateConfig {
2318

2419
@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;
5140

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);
5844

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
6249

63-
return new RestTemplate(requestFactory);
50+
return new RestTemplate(factory);
6451
}
6552
}
1.21 KB
Binary file not shown.
2.67 KB
Binary file not shown.

target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
com/tennis/config/RestTemplateConfig.class
12
com/tennis/entity/MatchPrediction.class
23
com/tennis/controller/TennisPredictionController.class
34
com/tennis/TennisMatchPredictionApplication.class
@@ -10,6 +11,7 @@ com/tennis/service/DataInitializationService.class
1011
com/tennis/entity/Player.class
1112
com/tennis/entity/Match.class
1213
com/tennis/repository/HeadToHeadRepository.class
14+
com/tennis/config/RestTemplateConfig$1.class
1315
com/tennis/service/PredictionService.class
1416
com/tennis/service/MockFlashScoreService.class
1517
com/tennis/repository/PlayerRepository.class

0 commit comments

Comments
 (0)