从Java连接Elasticsearch时获取java.net.ConnectException

时间:2018-11-16 20:16:36

标签: java ssl elasticsearch ssl-certificate elastic-stack

我正在使用以下代码使用jks和基本身份验证从Java连接Elasticsearch。但是我在搜索时最后遇到了javax.net.ssl.SSLHandshakeException:一般SSLEngine问题。

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(esConfig.getEsUserName(), esConfig.getEsPassword()));

            InputStream actualPath = this.getClass().getClassLoader().getResourceAsStream(esConfig.getJksFileName());

KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(actualPath, esConfig.getKeystorePassword().toCharArray() );

final SSLContext sslContext = SSLContexts.custom()
                    .loadTrustMaterial(truststore, null)
                    .build();
RestClientBuilder builder = RestClient.builder(new HttpHost(esConfig.getEsHost()[0], esConfig.getEsPort(), "https"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(sslContext);

                        }
                    });

client = new RestHighLevelClient(builder);

这是创建客户端的代码,我在那里没有看到任何异常,但是当使用以下代码执行搜索时,出现此错误。

searchResponse = client.search(searchRequest);

感谢您的帮助。 JKS pasword正确,因为能够使用该密码打开jks文件

0 个答案:

没有答案