|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Method; |
20 | 20 | import java.net.http.HttpClient; |
21 | | -import java.security.KeyManagementException; |
22 | | -import java.security.NoSuchAlgorithmException; |
23 | | -import java.security.cert.X509Certificate; |
24 | | -import java.time.Duration; |
25 | 21 | import java.util.ArrayList; |
26 | 22 | import java.util.List; |
27 | 23 | import java.util.Set; |
28 | | -import java.util.concurrent.TimeUnit; |
29 | | -import java.util.stream.Collectors; |
30 | | - |
31 | | -import javax.net.ssl.HostnameVerifier; |
32 | | -import javax.net.ssl.SSLContext; |
33 | | -import javax.net.ssl.SSLSession; |
34 | | -import javax.net.ssl.SSLSocketFactory; |
35 | | -import javax.net.ssl.TrustManager; |
36 | | -import javax.net.ssl.X509TrustManager; |
37 | 24 |
|
38 | 25 | import feign.Capability; |
39 | 26 | import feign.Client; |
|
42 | 29 | import feign.Target; |
43 | 30 | import feign.hc5.ApacheHttp5Client; |
44 | 31 | import feign.http2client.Http2Client; |
45 | | -import feign.okhttp.OkHttpClient; |
46 | | -import jakarta.annotation.PreDestroy; |
47 | | -import okhttp3.ConnectionPool; |
48 | | -import okhttp3.Protocol; |
49 | 32 | import org.apache.commons.logging.Log; |
50 | 33 | import org.apache.commons.logging.LogFactory; |
51 | 34 |
|
@@ -231,121 +214,6 @@ public String resolveCircuitBreakerName(String feignClientName, Target<?> target |
231 | 214 |
|
232 | 215 | } |
233 | 216 |
|
234 | | - // the following configuration is for alternate feign clients if |
235 | | - // SC loadbalancer is not on the class path. |
236 | | - // see corresponding configurations in FeignLoadBalancerAutoConfiguration |
237 | | - // for load-balanced clients. |
238 | | - @Configuration(proxyBeanMethods = false) |
239 | | - @ConditionalOnClass(OkHttpClient.class) |
240 | | - @ConditionalOnMissingBean(okhttp3.OkHttpClient.class) |
241 | | - @ConditionalOnProperty("spring.cloud.openfeign.okhttp.enabled") |
242 | | - protected static class OkHttpFeignConfiguration { |
243 | | - |
244 | | - private okhttp3.OkHttpClient okHttpClient; |
245 | | - |
246 | | - @Bean |
247 | | - @ConditionalOnMissingBean |
248 | | - public okhttp3.OkHttpClient.Builder okHttpClientBuilder() { |
249 | | - return new okhttp3.OkHttpClient.Builder(); |
250 | | - } |
251 | | - |
252 | | - @Bean |
253 | | - @ConditionalOnMissingBean(ConnectionPool.class) |
254 | | - public ConnectionPool httpClientConnectionPool(FeignHttpClientProperties httpClientProperties) { |
255 | | - int maxTotalConnections = httpClientProperties.getMaxConnections(); |
256 | | - long timeToLive = httpClientProperties.getTimeToLive(); |
257 | | - TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit(); |
258 | | - return new ConnectionPool(maxTotalConnections, timeToLive, ttlUnit); |
259 | | - } |
260 | | - |
261 | | - @Bean |
262 | | - public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, ConnectionPool connectionPool, |
263 | | - FeignHttpClientProperties httpClientProperties) { |
264 | | - boolean followRedirects = httpClientProperties.isFollowRedirects(); |
265 | | - int connectTimeout = httpClientProperties.getConnectionTimeout(); |
266 | | - boolean disableSslValidation = httpClientProperties.isDisableSslValidation(); |
267 | | - Duration readTimeout = httpClientProperties.getOkHttp().getReadTimeout(); |
268 | | - List<Protocol> protocols = httpClientProperties.getOkHttp() |
269 | | - .getProtocols() |
270 | | - .stream() |
271 | | - .map(Protocol::valueOf) |
272 | | - .collect(Collectors.toList()); |
273 | | - if (disableSslValidation) { |
274 | | - disableSsl(builder); |
275 | | - } |
276 | | - this.okHttpClient = builder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) |
277 | | - .followRedirects(followRedirects) |
278 | | - .readTimeout(readTimeout) |
279 | | - .connectionPool(connectionPool) |
280 | | - .protocols(protocols) |
281 | | - .build(); |
282 | | - return this.okHttpClient; |
283 | | - } |
284 | | - |
285 | | - private void disableSsl(okhttp3.OkHttpClient.Builder builder) { |
286 | | - try { |
287 | | - X509TrustManager disabledTrustManager = new DisableValidationTrustManager(); |
288 | | - TrustManager[] trustManagers = new TrustManager[1]; |
289 | | - trustManagers[0] = disabledTrustManager; |
290 | | - SSLContext sslContext = SSLContext.getInstance("SSL"); |
291 | | - sslContext.init(null, trustManagers, new java.security.SecureRandom()); |
292 | | - SSLSocketFactory disabledSSLSocketFactory = sslContext.getSocketFactory(); |
293 | | - builder.sslSocketFactory(disabledSSLSocketFactory, disabledTrustManager); |
294 | | - builder.hostnameVerifier(new TrustAllHostnames()); |
295 | | - } |
296 | | - catch (NoSuchAlgorithmException | KeyManagementException e) { |
297 | | - LOG.warn("Error setting SSLSocketFactory in OKHttpClient", e); |
298 | | - } |
299 | | - } |
300 | | - |
301 | | - @PreDestroy |
302 | | - public void destroy() { |
303 | | - if (this.okHttpClient != null) { |
304 | | - this.okHttpClient.dispatcher().executorService().shutdown(); |
305 | | - this.okHttpClient.connectionPool().evictAll(); |
306 | | - } |
307 | | - } |
308 | | - |
309 | | - @Bean |
310 | | - @ConditionalOnMissingBean(Client.class) |
311 | | - public Client feignClient(okhttp3.OkHttpClient client) { |
312 | | - return new OkHttpClient(client); |
313 | | - } |
314 | | - |
315 | | - /** |
316 | | - * A {@link X509TrustManager} that does not validate SSL certificates. |
317 | | - */ |
318 | | - class DisableValidationTrustManager implements X509TrustManager { |
319 | | - |
320 | | - @Override |
321 | | - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { |
322 | | - } |
323 | | - |
324 | | - @Override |
325 | | - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { |
326 | | - } |
327 | | - |
328 | | - @Override |
329 | | - public X509Certificate[] getAcceptedIssuers() { |
330 | | - return new X509Certificate[0]; |
331 | | - } |
332 | | - |
333 | | - } |
334 | | - |
335 | | - /** |
336 | | - * A {@link HostnameVerifier} that does not validate any hostnames. |
337 | | - */ |
338 | | - class TrustAllHostnames implements HostnameVerifier { |
339 | | - |
340 | | - @Override |
341 | | - public boolean verify(String s, SSLSession sslSession) { |
342 | | - return true; |
343 | | - } |
344 | | - |
345 | | - } |
346 | | - |
347 | | - } |
348 | | - |
349 | 217 | // the following configuration is for alternate feign clients if |
350 | 218 | // SC loadbalancer is not on the class path. |
351 | 219 | // see corresponding configurations in FeignLoadBalancerAutoConfiguration |
|
0 commit comments