es连接配置支持外部证书、证书密码加载

This commit is contained in:
gaoqr
2024-11-04 10:42:06 +08:00
parent d7c4fc1df3
commit 8e21ef95f3
@@ -1,6 +1,6 @@
package com.cf.imes.framework.es.config;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.PathUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
@@ -146,7 +146,7 @@ public class ChenfengElasticsearchAutoConfiguration {
}
if (properties.isSecurityHttpSslEnable()) {
// 开启ssl配置连接证书
httpAsyncClientBuilder.setSSLContext(buildSSLContext(properties.getCertificatePath())).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
httpAsyncClientBuilder.setSSLContext(buildSSLContext(properties)).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
}
return httpAsyncClientBuilder;
};
@@ -156,16 +156,16 @@ public class ChenfengElasticsearchAutoConfiguration {
/**
* 构建ssl请求信息
*
* @param certificatePath 证书地址
* @param properties es配置
* @return
*/
private SSLContext buildSSLContext(String certificatePath) {
private SSLContext buildSSLContext(EsProperties properties) {
SSLContext sslContext = null;
try {
Path trustStorePath = Paths.get(certificatePath);
Path trustStorePath = Paths.get(properties.getCertificatePath());
KeyStore trustStore = KeyStore.getInstance("pkcs12");
try (InputStream is = FileUtil.getInputStream(trustStorePath)) {
trustStore.load(is, "elastic".toCharArray());
try (InputStream is = PathUtil.getInputStream(trustStorePath)) {
trustStore.load(is, properties.getPassword().toCharArray());
}
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);