|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.openfeign.clientconfig; |
| 18 | + |
| 19 | +import java.security.KeyManagementException; |
| 20 | +import java.security.NoSuchAlgorithmException; |
| 21 | +import java.security.SecureRandom; |
| 22 | +import java.security.cert.CertificateException; |
| 23 | +import java.security.cert.X509Certificate; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import javax.annotation.PreDestroy; |
| 27 | +import javax.net.ssl.SSLContext; |
| 28 | +import javax.net.ssl.TrustManager; |
| 29 | +import javax.net.ssl.X509TrustManager; |
| 30 | + |
| 31 | +import org.apache.commons.logging.Log; |
| 32 | +import org.apache.commons.logging.LogFactory; |
| 33 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 34 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 35 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 36 | +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; |
| 37 | +import org.apache.hc.client5.http.io.HttpClientConnectionManager; |
| 38 | +import org.apache.hc.client5.http.socket.LayeredConnectionSocketFactory; |
| 39 | +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder; |
| 40 | +import org.apache.hc.core5.http.io.SocketConfig; |
| 41 | +import org.apache.hc.core5.http.ssl.TLS; |
| 42 | +import org.apache.hc.core5.io.CloseMode; |
| 43 | +import org.apache.hc.core5.pool.PoolConcurrencyPolicy; |
| 44 | +import org.apache.hc.core5.pool.PoolReusePolicy; |
| 45 | +import org.apache.hc.core5.ssl.SSLContexts; |
| 46 | +import org.apache.hc.core5.util.TimeValue; |
| 47 | +import org.apache.hc.core5.util.Timeout; |
| 48 | + |
| 49 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 50 | +import org.springframework.cloud.openfeign.support.FeignHttpClientProperties; |
| 51 | +import org.springframework.context.annotation.Bean; |
| 52 | +import org.springframework.context.annotation.Configuration; |
| 53 | + |
| 54 | +/** |
| 55 | + * Default configuration for {@link CloseableHttpClient}. |
| 56 | + * |
| 57 | + * @author Nguyen Ky Thanh |
| 58 | + */ |
| 59 | +@Configuration(proxyBeanMethods = false) |
| 60 | +@ConditionalOnMissingBean(CloseableHttpClient.class) |
| 61 | +public class HttpClient5FeignConfiguration { |
| 62 | + |
| 63 | + private static final Log LOG = LogFactory.getLog(HttpClient5FeignConfiguration.class); |
| 64 | + |
| 65 | + private CloseableHttpClient httpClient5; |
| 66 | + |
| 67 | + @Bean |
| 68 | + @ConditionalOnMissingBean(HttpClientConnectionManager.class) |
| 69 | + public HttpClientConnectionManager hc5ConnectionManager( |
| 70 | + FeignHttpClientProperties httpClientProperties) { |
| 71 | + return PoolingHttpClientConnectionManagerBuilder.create() |
| 72 | + .setSSLSocketFactory(httpsSSLConnectionSocketFactory( |
| 73 | + httpClientProperties.isDisableSslValidation())) |
| 74 | + .setMaxConnTotal(httpClientProperties.getMaxConnections()) |
| 75 | + .setMaxConnPerRoute(httpClientProperties.getMaxConnectionsPerRoute()) |
| 76 | + .setConnPoolPolicy(PoolReusePolicy.valueOf( |
| 77 | + httpClientProperties.getHc5().getPoolReusePolicy().name())) |
| 78 | + .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.valueOf( |
| 79 | + httpClientProperties.getHc5().getPoolConcurrencyPolicy().name())) |
| 80 | + .setConnectionTimeToLive( |
| 81 | + TimeValue.of(httpClientProperties.getTimeToLive(), |
| 82 | + httpClientProperties.getTimeToLiveUnit())) |
| 83 | + .setDefaultSocketConfig(SocketConfig.custom() |
| 84 | + .setSoTimeout(Timeout.of( |
| 85 | + httpClientProperties.getHc5().getSocketTimeout(), |
| 86 | + httpClientProperties.getHc5().getSocketTimeoutUnit())) |
| 87 | + .build()) |
| 88 | + .build(); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + public CloseableHttpClient httpClient5(HttpClientConnectionManager connectionManager, |
| 93 | + FeignHttpClientProperties httpClientProperties) { |
| 94 | + httpClient5 = HttpClients.custom().disableCookieManagement().useSystemProperties() |
| 95 | + .setConnectionManager(connectionManager).evictExpiredConnections() |
| 96 | + .setDefaultRequestConfig( |
| 97 | + RequestConfig.custom() |
| 98 | + .setConnectTimeout(Timeout.of( |
| 99 | + httpClientProperties.getConnectionTimeout(), |
| 100 | + TimeUnit.MILLISECONDS)) |
| 101 | + .setRedirectsEnabled( |
| 102 | + httpClientProperties.isFollowRedirects()) |
| 103 | + .build()) |
| 104 | + .build(); |
| 105 | + return httpClient5; |
| 106 | + } |
| 107 | + |
| 108 | + @PreDestroy |
| 109 | + public void destroy() { |
| 110 | + if (httpClient5 != null) { |
| 111 | + httpClient5.close(CloseMode.GRACEFUL); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private LayeredConnectionSocketFactory httpsSSLConnectionSocketFactory( |
| 116 | + boolean isDisableSslValidation) { |
| 117 | + final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = SSLConnectionSocketFactoryBuilder |
| 118 | + .create().setTlsVersions(TLS.V_1_3, TLS.V_1_2); |
| 119 | + |
| 120 | + if (isDisableSslValidation) { |
| 121 | + try { |
| 122 | + final SSLContext sslContext = SSLContext.getInstance("SSL"); |
| 123 | + sslContext.init(null, |
| 124 | + new TrustManager[] { new DisabledValidationTrustManager() }, |
| 125 | + new SecureRandom()); |
| 126 | + sslConnectionSocketFactoryBuilder.setSslContext(sslContext); |
| 127 | + } |
| 128 | + catch (NoSuchAlgorithmException e) { |
| 129 | + LOG.warn("Error creating SSLContext", e); |
| 130 | + } |
| 131 | + catch (KeyManagementException e) { |
| 132 | + LOG.warn("Error creating SSLContext", e); |
| 133 | + } |
| 134 | + } |
| 135 | + else { |
| 136 | + sslConnectionSocketFactoryBuilder |
| 137 | + .setSslContext(SSLContexts.createSystemDefault()); |
| 138 | + } |
| 139 | + |
| 140 | + return sslConnectionSocketFactoryBuilder.build(); |
| 141 | + } |
| 142 | + |
| 143 | + static class DisabledValidationTrustManager implements X509TrustManager { |
| 144 | + |
| 145 | + DisabledValidationTrustManager() { |
| 146 | + } |
| 147 | + |
| 148 | + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) |
| 149 | + throws CertificateException { |
| 150 | + } |
| 151 | + |
| 152 | + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) |
| 153 | + throws CertificateException { |
| 154 | + } |
| 155 | + |
| 156 | + public X509Certificate[] getAcceptedIssuers() { |
| 157 | + return null; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments