ElasticSearch8之后的版本默认是通过https访问的文章来源:https://www.toymoban.com/news/detail-516488.html
通过https访问
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
@Configuration
public class ElasticsearchConfig {
@Bean
public ElasticsearchClient client() throws Exception {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//设置账号密码
credentialsProvider.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials("elastic", "elastic"));
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
final MyHostnameVerifier hostnameVerifier = new MyHostnameVerifier();
RestClient restClient = RestClient.builder(new HttpHost("127.0.0.1", 9200, "https"))
.setHttpClientConfigCallback(httpClientBuilder->httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLContext(sslContext)
.setSSLHostnameVerifier(hostnameVerifier)
).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
return client;
}
/**
* 重写HostnameVerifier接口
*/
private static class MyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
private static class MyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}
https改为http
也可以改为http访问,修改配置文件elasticsearch.yml 把下面配置改为false 重启就好了。文章来源地址https://www.toymoban.com/news/detail-516488.html
xpack.security.http.ssl:
enabled: false
到了这里,关于ElasticSearch8.+ 通过https访问的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!