当前位置: 首页 > article >正文

springboot2.7集成es8 https方式绕过证书访问集群

版本说明

项目Value
spring-boot2.7.13
es8.12.2

配置文件

spring:
   elasticsearch:
     uris: https://192.168.1.110:30920
     username: elastic
     password: 123456

依赖文件

子pom.xml引入elasticsearch-java依赖

      <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.12.2</version>
        </dependency>

如果出现版本问题,父pom.xml修改依赖声明

      <jakarta-json.version>2.0.1</jakarta-json.version>
      <elasticsearch.version>8.12.2</elasticsearch.version>

   
    <dependencyManagement>
       <dependencies>
       <dependency>
                <groupId>jakarta.json</groupId>
                <artifactId>jakarta.json-api</artifactId>
                <version>${jakarta-json.version}</version>
            </dependency>

            <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
                <version>${elasticsearch.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置类


import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.manlan.common.http.URLInfo;
import com.manlan.common.http.URLUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.ssl.SSLContextBuilder;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLContext;
import java.util.List;

@Configuration
@Slf4j
public class ElasticSearchConfig {

    @Value("${spring.elasticsearch.uris}")
    private String uris;

    @Value("${spring.elasticsearch.username}")
    private String username;

    @Value("${spring.elasticsearch.password}")
    private String password;

    @Bean
    public RestClient restClient() throws Exception {
        List<String> urlList = StrUtil.split(uris, StrPool.COMMA);
        HttpHost[] httpHosts = new HttpHost[urlList.size()];
        for (int i = 0; i < urlList.size(); i++) {
            URLInfo urlInfo = URLUtil.str2UrlInfo(urlList.get(i));
            httpHosts[i] = new HttpHost(urlInfo.getHost(), urlInfo.getPort(), urlInfo.getProtocol());
        }

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        SSLContext sslContext = SSLContextBuilder.create()
            .loadTrustMaterial((chain, authType) -> true)
            .build();

        return RestClient.builder(httpHosts)
            .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .setSSLContext(sslContext)
                .setDefaultCredentialsProvider(credentialsProvider))
            .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                .setConnectTimeout(5000)
                .setSocketTimeout(60000)).build();
    }


    @Bean
    public ElasticsearchClient elasticsearchClient() throws Exception {
        RestClient restClient = restClient();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }


}

工具类

public class URLUtil extends cn.hutool.core.util.URLUtil {

    public static URLInfo str2UrlInfo(String url) {
        if (!StrUtil.startWithAny(url, "https://", "http://")) {
            url = HTTP + url;
        }
        URL urlForHttp = toUrlForHttp(url);
        int port = urlForHttp.getPort();
        String protocol = urlForHttp.getProtocol();
        if (port == -1 && StrUtil.equals("https", protocol)) {
            port = 443;
        }
        if (port == -1 && StrUtil.equals("http", protocol)) {
            port = 80;
        }
        return new URLInfo(protocol, urlForHttp.getHost(), port);
    }

}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class URLInfo {
    private String protocol;
    private String host;
    private Integer port;
}

http://www.kler.cn/a/274693.html

相关文章:

  • tslib(触摸屏输入设备的轻量级库)的学习、编译及测试记录
  • 【西南大学主办】第九届电子信息技术与计算机工程国际学术会议(EITCE 2025)
  • HUAWEI-eNSP交换机链路聚合(手动负载分担模式)
  • centos7下docker 容器实现redis主从同步
  • 回归预测 | MATLAB实现CNN-BiGRU-Attention卷积神经网络结合双向门控循环单元融合注意力机制多输入单输出回归预测
  • [LeetCode-Python版] 定长滑动窗口1(1456 / 643 / 1343 / 2090 / 2379)
  • 32串口学习
  • 【图论】【深度优先搜索】【换根法】2858. 可以到达每一个节点的最少边反转次数
  • SpringMVC的执行原理
  • 「实战应用」如何用DHTMLX构建自定义JavaScript甘特图(二)
  • React简介
  • 在Ubuntu20.04(原为cuda12.0, gcc9.几版本和g++9.几版本)下先安装cuda9.0后再配置gcc-5环境
  • 图书馆管理系统 1.架构项目以及加搭建项目
  • centos安装docker-compose
  • Selenium不同版本配置自动下载驱动及打包细节
  • Spring的炼气之路(炼气三层)
  • 3、java虚拟机-类的生命周期-初始化阶段(与程序员有关)
  • JRTLIS登录
  • 前端小白的学习之路(lessscss)
  • 百度交易中台之系统对账篇
  • 如何利用机器学习和Python编写预测模型来预测设备故障
  • 代码随想录阅读笔记-字符串【翻转字符串中单词】
  • Unity构建详解(2)——SBP的初始设置和脚本编译
  • 【自记录】VS2022编译OpenSSL1.0.2u
  • 电装DENSO 嵌入式岗笔试
  • Qt + HTTP 线程交互类封装