本文简单示例 Java 实例化一个可调用 https 请求的 RestTemplate:

public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1"},
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }

 

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2022-01-29
  • 2021-12-26
  • 2022-12-23
  • 2021-08-26
  • 2021-10-27
  • 2021-12-05
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2022-02-19
  • 2022-12-23
  • 2021-11-29
  • 2021-10-01
相关资源
相似解决方案