springboot2.7集成es8 https方式绕过证书访问集群
版本说明
项目 | Value |
---|---|
spring-boot | 2.7.13 |
es | 8.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;
}