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

springboot使用ssl连接elasticsearch

使用es时ssl证书报错 unable to find valid certification path to requested target

1.依赖

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>

2.配置证书

ssl证书转换,使用的jdk工具将cer证书转换为jks证书

keytool -import -alias mycert -file mycert.cer -keystore mytruststore.jks -storepass test123..

application.yaml配置

spring:  elasticsearch:key-store: classpath:ssl/truststore.jks#转换证书时的密码key-store-password: test123..username: adminpassword: xxx#不用带协议uris: xxxxx:9200

配置类

package com.echosell.spider.appspider.config;import com.echosell.spider.appspider.entity.properties.EsProperties;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.security.KeyStore;/*** @author zzy* @project echosell-spider* @description es配置* @date 2025年01月20日**/
@Configuration
@Slf4j
public class ElasticsearchTemplateConfig {@AutowiredEsProperties esProperties;@AutowiredResourceLoader resourceLoader;@Beanpublic RestHighLevelClient restHighLevelClient() throws Exception {ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo(esProperties.getUris()).usingSsl(createSSLContext(esProperties.getKeyStore(), esProperties.getKeyStorePassword())).withBasicAuth(esProperties.getUsername(), esProperties.getPassword()).build();return RestClients.create(clientConfiguration).rest();}@Beanpublic ElasticsearchRestTemplate elasticsearchRestTemplate(RestHighLevelClient restHighLevelClient){return new ElasticsearchRestTemplate(restHighLevelClient);}private SSLContext createSSLContext(String keyStorePath, String keyStorePassword) throws Exception {
//        // 加载密钥库KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());Resource resource = resourceLoader.getResource(keyStorePath);// 读取文件内容...try (FileInputStream fileInputStream = new FileInputStream(resource.getFile())) {trustStore.load(fileInputStream, keyStorePassword.toCharArray());}// 创建信任管理器工厂TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(trustStore);// 初始化 SSLContextSSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, trustManagerFactory.getTrustManagers(), null);return sslContext;}}

3.信任所有证书(无证书使用)

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}
}}, new SecureRandom());

直接使用ElasticsearchRestTemplate即可

4.介绍

网上百度无结果,查看了部分源码发现 RestHighLevelClient 使用的SSLContext,且默认使用的系统默认证书 ,将自己的证书导入 SSLContext,封装到RestHighLevelClient即可。

http://www.lryc.cn/news/525687.html

相关文章:

  • Linux内核中的InfiniBand核心驱动:verbs.c分析
  • 把网站程序数据上传到服务器的方法和注意事项
  • 完全平方数——唯一分解定理
  • (详细)Springboot 整合动态多数据源 这里有mysql(分为master 和 slave) 和oracle,根据不同路径适配不同数据源
  • mock可视化生成前端代码
  • Spring Boot(6)解决ruoyi框架连续快速发送post请求时,弹出“数据正在处理,请勿重复提交”提醒的问题
  • 鸿蒙Harmony json转对象(1)
  • 常见的RocketMQ面试题及其简要答案
  • C#Object类型的索引,序列化和反序列化
  • Unity3D项目开发中的资源加密详解
  • 微调Qwen2:7B模型,加入未知信息语料
  • 【Ubuntu】安装SSH启用远程连接
  • 【理论】测试开发工程师进阶路线
  • 【BQ3568HM开发板】如何在OpenHarmony上通过校园网的上网认证
  • 動態住宅IP提升網站訪問成功率
  • 2024年博客之星主题创作|2024年蓝桥杯与数学建模年度总结与心得
  • Spring Boot/MVC
  • 由于请求的竞态问题,前端仔喜提了一个bug
  • 【Day25 LeetCode】贪心Ⅲ
  • 蓝桥杯练习日常|递归-进制转换
  • AI Agent:深度解析与未来展望
  • 《SwinIR:使用Swin-Transformer图像恢复》学习笔记
  • 如何在Nginx服务器上配置访问静态文件目录并提供文件下载功能
  • ansible自动化运维实战--script、unarchive和shell模块(6)
  • 理解深度学习pytorch框架中的线性层
  • 电路研究9.2——合宙Air780EP使用AT指令
  • Qt数据库相关操作
  • 2025-01-22 Unity Editor 1 —— MenuItem 入门
  • 解锁C#编程新姿势:Z.ExtensionMethods入门秘籍
  • 不使用 JS 纯 CSS 获取屏幕宽高