如何在Java中禁用Elasticsearch RestClient v6.7.0的SSL验证

8ehkhllq  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(303)

我正在尝试连接到ssh隧道后面的elasticsearch示例。elasticsearch示例的域是*.ap-south-1.es.amazonaws.com,而在本地隧道上,我通过localhost:9201连接。
下面是我用来连接到elasticsearch的代码

RestHighLevelClient(RestClient.builder(HttpHost("localhost", 9201, "https")))

我收到以下错误

javax.net.ssl.SSLPeerUnverifiedException: Host name 'localhost' does not match the certificate subject provided by the peer (CN=*.ap-south-1.es.amazonaws.com)

当我使用PHP-Elasticsearch时,我得到了这个错误,我使用

$esClient->setSSLVerification(false);

我希望为Java RestClient找到一个类似的方法。

x33g5p2x

x33g5p2x1#

由于证书中的主机名不是localhost,因此您将遇到此问题,因此要解决此问题,您需要禁用SSL主机名验证,通过执行以下操作,始终返回true,这将跳过验证。

RestClientBuilder restClientBuilder =  RestClient.builder(HttpHost);
restClientBuilder.setHttpClientConfigCallback(httpAsyncClientBuilder ->
   httpAsyncClientBuilder.setSSLHostnameVerifier((s, sslSession) -> true));
new RestHighLevelClient(restClientBuilder);
5m1hhzi4

5m1hhzi42#

我希望这会给予一个完整的答案。
希望这对你有帮助,我也遇到过同样的问题,这就是我如何解决的。

@Bean
        public RestHighLevelClient createSimpleElasticClient() throws Exception {
            try {
                SSLContextBuilder sslBuilder = SSLContexts.custom()
                        .loadTrustMaterial(null, (x509Certificates, s) -> true);
                        final SSLContext sslContext = sslBuilder.build();
                RestHighLevelClient client = new RestHighLevelClient(RestClient
                        .builder(new HttpHost(hostNameOrLoadbalancerURL, 443, "https")) 
//port number is given as 443 since its https schema
                        .setHttpClientConfigCallback(new HttpClientConfigCallback() {
                            @Override
                            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                                return httpClientBuilder
                                         .setSSLContext(sslContext)
                                         .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
                            }
                        })
                        .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                            @Override
                            public RequestConfig.Builder customizeRequestConfig(
                                    RequestConfig.Builder requestConfigBuilder) {
                                return requestConfigBuilder.setConnectTimeout(5000)
                                        .setSocketTimeout(120000);
                            }
                        }));
                System.out.println("elasticsearch client created");
                return client;
            } catch (Exception e) {
                System.out.println(e);
                throw new Exception("Could not create an elasticsearch client!!");
            }
        }
ztmd8pv5

ztmd8pv53#

为此,您必须禁用一个设置,该设置将验证主机名是否与您提供的名称一致。这是Apache中HTTPClient的一个错误,您必须虚拟化在setSSLHostnameVerifier方法中验证的主机名,如下所示。
虽然这段代码是用Kotlin编写的,但是可以很容易地编写Java替代品

val builder = RestClient.builder(host).setHttpClientConfigCallback { httpAsyncClientBuilder ->
            httpAsyncClientBuilder.setSSLHostnameVerifier { _, _ -> true }
        }

这将始终覆盖验证主机名为true的设置。
下面是Java代码参考。

RestClientBuilder builder =  RestClient.builder(host);
builder.setHttpClientConfigCallback(httpAsyncClientBuilder ->
   httpAsyncClientBuilder.setSSLHostnameVerifier((host, sslSession) -> true));
new RestHighLevelClient(builder);
fkvaft9z

fkvaft9z4#

Elastic在Kotlin上使用Sping Boot 禁用SSL验证的完整工作示例

import org.apache.http.conn.ssl.NoopHostnameVerifier
import org.apache.http.conn.ssl.TrustAllStrategy
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
import org.apache.http.ssl.SSLContexts
import org.elasticsearch.client.RestClientBuilder
import org.springframework.boot.autoconfigure.elasticsearch.RestClientBuilderCustomizer

@Configuration
@ConditionalOnProperty(name = ["spring.elasticsearch.rest.ssl.disable"], havingValue = "DISABLE_SSL")
class ElasticSSLCertDisableConfig {

    private val sslContext = SSLContexts.custom()
        .loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
        .build()

    @Bean
    fun restClientBuilderCustomizer() = object : RestClientBuilderCustomizer {
        override fun customize(builder: HttpAsyncClientBuilder) {
            builder.setSSLContext(sslContext)
            builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
        }

        override fun customize(builder: RestClientBuilder) {}
    }
}

注意:仅将其用于开发环境。

相关问题