17
17
package org .springframework .cloud .openfeign ;
18
18
19
19
import java .lang .reflect .Method ;
20
+ import java .security .KeyManagementException ;
21
+ import java .security .NoSuchAlgorithmException ;
22
+ import java .security .cert .X509Certificate ;
20
23
import java .time .Duration ;
21
24
import java .util .ArrayList ;
22
25
import java .util .List ;
23
26
import java .util .concurrent .TimeUnit ;
24
27
28
+ import javax .net .ssl .HostnameVerifier ;
29
+ import javax .net .ssl .SSLContext ;
30
+ import javax .net .ssl .SSLSession ;
31
+ import javax .net .ssl .SSLSocketFactory ;
32
+ import javax .net .ssl .TrustManager ;
33
+ import javax .net .ssl .X509TrustManager ;
34
+
25
35
import com .fasterxml .jackson .databind .Module ;
26
36
import feign .Capability ;
27
37
import feign .Client ;
31
41
import feign .okhttp .OkHttpClient ;
32
42
import jakarta .annotation .PreDestroy ;
33
43
import okhttp3 .ConnectionPool ;
44
+ import org .apache .commons .logging .Log ;
45
+ import org .apache .commons .logging .LogFactory ;
34
46
35
47
import org .springframework .beans .factory .annotation .Autowired ;
36
48
import org .springframework .beans .factory .annotation .Value ;
43
55
import org .springframework .cloud .client .actuator .HasFeatures ;
44
56
import org .springframework .cloud .client .circuitbreaker .CircuitBreaker ;
45
57
import org .springframework .cloud .client .circuitbreaker .CircuitBreakerFactory ;
46
- import org .springframework .cloud .commons .httpclient .OkHttpClientConnectionPoolFactory ;
47
- import org .springframework .cloud .commons .httpclient .OkHttpClientFactory ;
48
58
import org .springframework .cloud .openfeign .security .OAuth2AccessTokenInterceptor ;
49
59
import org .springframework .cloud .openfeign .support .FeignEncoderProperties ;
50
60
import org .springframework .cloud .openfeign .support .FeignHttpClientProperties ;
81
91
FeignEncoderProperties .class })
82
92
public class FeignAutoConfiguration {
83
93
94
+ private static final Log LOG = LogFactory .getLog (FeignAutoConfiguration .class );
95
+
84
96
@ Autowired (required = false )
85
97
private List <FeignClientSpecification > configurations = new ArrayList <>();
86
98
@@ -205,29 +217,52 @@ protected static class OkHttpFeignConfiguration {
205
217
206
218
private okhttp3 .OkHttpClient okHttpClient ;
207
219
220
+ @ Bean
221
+ @ ConditionalOnMissingBean
222
+ public okhttp3 .OkHttpClient .Builder okHttpClientBuilder () {
223
+ return new okhttp3 .OkHttpClient .Builder ();
224
+ }
225
+
208
226
@ Bean
209
227
@ ConditionalOnMissingBean (ConnectionPool .class )
210
- public ConnectionPool httpClientConnectionPool (FeignHttpClientProperties httpClientProperties ,
211
- OkHttpClientConnectionPoolFactory connectionPoolFactory ) {
228
+ public ConnectionPool httpClientConnectionPool (FeignHttpClientProperties httpClientProperties ) {
212
229
int maxTotalConnections = httpClientProperties .getMaxConnections ();
213
230
long timeToLive = httpClientProperties .getTimeToLive ();
214
231
TimeUnit ttlUnit = httpClientProperties .getTimeToLiveUnit ();
215
- return connectionPoolFactory . create (maxTotalConnections , timeToLive , ttlUnit );
232
+ return new ConnectionPool (maxTotalConnections , timeToLive , ttlUnit );
216
233
}
217
234
218
235
@ Bean
219
- public okhttp3 .OkHttpClient client (OkHttpClientFactory httpClientFactory , ConnectionPool connectionPool ,
236
+ public okhttp3 .OkHttpClient client (okhttp3 . OkHttpClient . Builder builder , ConnectionPool connectionPool ,
220
237
FeignHttpClientProperties httpClientProperties ) {
221
238
boolean followRedirects = httpClientProperties .isFollowRedirects ();
222
239
int connectTimeout = httpClientProperties .getConnectionTimeout ();
223
240
boolean disableSslValidation = httpClientProperties .isDisableSslValidation ();
224
241
Duration readTimeout = httpClientProperties .getOkHttp ().getReadTimeout ();
225
- this .okHttpClient = httpClientFactory .createBuilder (disableSslValidation )
226
- .connectTimeout (connectTimeout , TimeUnit .MILLISECONDS ).followRedirects (followRedirects )
227
- .readTimeout (readTimeout ).connectionPool (connectionPool ).build ();
242
+ if (disableSslValidation ) {
243
+ disableSsl (builder );
244
+ }
245
+ this .okHttpClient = builder .connectTimeout (connectTimeout , TimeUnit .MILLISECONDS )
246
+ .followRedirects (followRedirects ).readTimeout (readTimeout ).connectionPool (connectionPool ).build ();
228
247
return this .okHttpClient ;
229
248
}
230
249
250
+ private void disableSsl (okhttp3 .OkHttpClient .Builder builder ) {
251
+ try {
252
+ X509TrustManager disabledTrustManager = new DisableValidationTrustManager ();
253
+ TrustManager [] trustManagers = new TrustManager [1 ];
254
+ trustManagers [0 ] = disabledTrustManager ;
255
+ SSLContext sslContext = SSLContext .getInstance ("SSL" );
256
+ sslContext .init (null , trustManagers , new java .security .SecureRandom ());
257
+ SSLSocketFactory disabledSSLSocketFactory = sslContext .getSocketFactory ();
258
+ builder .sslSocketFactory (disabledSSLSocketFactory , disabledTrustManager );
259
+ builder .hostnameVerifier (new TrustAllHostnames ());
260
+ }
261
+ catch (NoSuchAlgorithmException | KeyManagementException e ) {
262
+ LOG .warn ("Error setting SSLSocketFactory in OKHttpClient" , e );
263
+ }
264
+ }
265
+
231
266
@ PreDestroy
232
267
public void destroy () {
233
268
if (this .okHttpClient != null ) {
@@ -242,6 +277,38 @@ public Client feignClient(okhttp3.OkHttpClient client) {
242
277
return new OkHttpClient (client );
243
278
}
244
279
280
+ /**
281
+ * A {@link X509TrustManager} that does not validate SSL certificates.
282
+ */
283
+ class DisableValidationTrustManager implements X509TrustManager {
284
+
285
+ @ Override
286
+ public void checkClientTrusted (X509Certificate [] x509Certificates , String s ) {
287
+ }
288
+
289
+ @ Override
290
+ public void checkServerTrusted (X509Certificate [] x509Certificates , String s ) {
291
+ }
292
+
293
+ @ Override
294
+ public X509Certificate [] getAcceptedIssuers () {
295
+ return new X509Certificate [0 ];
296
+ }
297
+
298
+ }
299
+
300
+ /**
301
+ * A {@link HostnameVerifier} that does not validate any hostnames.
302
+ */
303
+ class TrustAllHostnames implements HostnameVerifier {
304
+
305
+ @ Override
306
+ public boolean verify (String s , SSLSession sslSession ) {
307
+ return true ;
308
+ }
309
+
310
+ }
311
+
245
312
}
246
313
247
314
// the following configuration is for alternate feign clients if
0 commit comments