paulwong

          httpClient連接自制SSL證書(shū)的rest服務(wù)

          通常如果rest服務(wù)支持https,需申請(qǐng)收費(fèi)的ssl證書(shū),但也可自制這種證書(shū)。
          httpClient進(jìn)行鏈接時(shí)要進(jìn)行相應(yīng)的設(shè)置, 主要是設(shè)置SSLContext中的TrustSelfSignedStrategy

          import java.security.KeyManagementException;
          import java.security.KeyStoreException;
          import java.security.NoSuchAlgorithmException;
          import java.util.concurrent.TimeUnit;

          import javax.net.ssl.SSLContext;

          import org.apache.http.client.config.RequestConfig;
          import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
          import org.apache.http.impl.client.CloseableHttpClient;
          import org.apache.http.impl.client.HttpClients;
          import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
          import org.apache.http.ssl.SSLContexts;
          import org.springframework.context.annotation.Bean;
          import org.springframework.context.annotation.Configuration;


          @Configuration
          public class HttpClientConfiguration {
              
              
              @Bean
              public PoolingHttpClientConnectionManager poolingHttpClientConnectionManager(AbstractProperties kycProperties) {
                  PoolingHttpClientConnectionManager result = 
                          new PoolingHttpClientConnectionManager(
                                  kycProperties.getHttpConnectionTimeToLiveMinu(), 
                                  TimeUnit.MINUTES
                              );
                  result.setMaxTotal(200);
                  result.setDefaultMaxPerRoute(20);
                  return result;
              }

              @Bean
              public RequestConfig requestConfig(AbstractProperties kycProperties) {
                  return RequestConfig
                              .custom()
                            .setConnectionRequestTimeout(kycProperties.getHttpConnectionTimeout())
                            .setConnectTimeout(kycProperties.getHttpConnectionTimeout())
                            .setSocketTimeout(kycProperties.getHttpConnectionTimeout())
                            .build();
              }
              
              @Bean
              public SSLContext sslContext() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {

                  return SSLContexts
                              .custom()
                              .loadTrustMaterial(nullnew TrustSelfSignedStrategy())
                              .build()
                              ;
              }

              @Bean
              public CloseableHttpClient httpClient(AbstractProperties kycProperties) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
                  return HttpClients
                            .custom()
          //                  .setConnectionManager(poolingHttpClientConnectionManager(null))
                            .setDefaultRequestConfig(requestConfig(null))
                            .setKeepAliveStrategy(
                                    new MyConnectionKeepAliveStrategy(
                                            kycProperties.getHttpConnectionTimeToLiveMinu(), 
                                            TimeUnit.MINUTES
                                        )
                             )
                            .setMaxConnTotal(200)
                            .setMaxConnPerRoute(20)
          //                  .setConnectionTimeToLive(
          //                          kycProperties.getHttpConnectionTimeToLiveMinu(), 
          //                          TimeUnit.MINUTES
          //                   )
                            .setSSLContext(sslContext())
                            .build();
              }

          }

          相應(yīng)設(shè)置
          http-connection-timeout: 30000
          http-connection-time-to-live-minu: 5

          posted on 2021-09-01 14:24 paulwong 閱讀(388) 評(píng)論(0)  編輯  收藏 所屬分類: HTTPCLIENT

          主站蜘蛛池模板: 博野县| 吴桥县| 甘孜县| 中山市| 鄂州市| 翁牛特旗| 旌德县| 全南县| 克什克腾旗| 德庆县| 桃园市| 舞阳县| 乐东| 邹城市| 富川| 清徐县| 定南县| 南郑县| 游戏| 巴彦淖尔市| 汾阳市| 社旗县| 谢通门县| 陇南市| 万荣县| 文安县| 辽阳市| 普兰店市| 贞丰县| 红桥区| 镇康县| 陆川县| 仪征市| 清徐县| 东乡县| 五峰| 天祝| 定兴县| 北流市| 新竹市| 当涂县|