簡體   English   中英

SSL / HTTPS 與低級別 Rest API 的連接?

[英]SSL / HTTPS Connection with Low Level Rest API?

我正在使用低級 rest API 訪問 AWS 中的 Elastic Search(AWS 服務)。 AWS 提供 HTTPS url。 以下代碼位於名為com.example.configurations.EsConfig的 SpringBoot 配置類中。

@Bean
public RestClient restClient() throws Exception {
    RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort))
            .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    HttpAsyncClientBuilder httpAsyncClientBuilder = null;
                    try {
                        httpAsyncClientBuilder = httpClientBuilder.setSSLContext(SSLContext.getDefault());
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                    }
                    return httpAsyncClientBuilder;
                }
            })
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000)
                    .setSocketTimeout(60000))
            .setMaxRetryTimeoutMillis(60000)
            .build();
    return restClient;
}

對於 esHost,我僅像 localhost 一樣提供 AWS Elasticsearch 端點 URL。

對於 esPort,在 HTTPS 的情況下為 443。

以下是錯誤消息:

2017-07-31 09:22:43.001  INFO 6239 --- [/O dispatcher 1] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.018  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.037  INFO 6239 --- [/O dispatcher 3] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.043  INFO 6239 --- [/O dispatcher 4] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 6] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 5] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.075  INFO 6239 --- [/O dispatcher 7] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer
2017-07-31 09:22:43.077  INFO 6239 --- [/O dispatcher 8] com.example.configurations.EsConfig      : org.apache.http.ConnectionClosedException: Connection closed
2017-07-31 09:22:43.099  INFO 6239 --- [/O dispatcher 2] com.example.configurations.EsConfig      : java.io.IOException: Connection reset by peer

我無法通過 SSL 連接獲取 RestClient。 請建議我通過 SSL 連接獲得 RestClient 需要什么。 謝謝

我在創建 HttpPost 對象作為第三個參數時解決了傳遞“https”的問題。

RestClient restClient = RestClient.builder(new HttpHost(esHost, esPort, "https"))
            .setHttpClientConfigCallback(httpClientBuilder -> {
                HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setSSLContext(sslcontext);
                return httpAsyncClientBuilder;
            })
            .build();

現在它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM