Common configuration - Encrypted communication

优质
小牛编辑
120浏览
2023-12-01

可以通过HttpClientConfigCallback配置加密传输。 参数org.apache.http.impl.nio.client.HttpAsyncClientBuilder 公开了多个方法来配置加密传输:setSSLContextsetSSLSessionStrategysetConnectionManager,以下是一个例子:

  1. KeyStore truststore = KeyStore.getInstance("jks");
  2. try (InputStream is = Files.newInputStream(keyStorePath)) {
  3. truststore.load(is, keyStorePass.toCharArray());
  4. }
  5. SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
  6. final SSLContext sslContext = sslBuilder.build();
  7. RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https"))
  8. .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  9. @Override
  10. public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  11. return httpClientBuilder.setSSLContext(sslContext);
  12. }
  13. });

如果没有提供明确的配置,则使用系统默认配置