|
@@ -1,38 +1,114 @@
|
|
package com.bowintek.practice.config;
|
|
package com.bowintek.practice.config;
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
|
+import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
|
|
|
|
+import co.elastic.clients.elasticsearch.indices.IndexState;
|
|
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
|
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
|
|
import co.elastic.clients.transport.ElasticsearchTransport;
|
|
import co.elastic.clients.transport.ElasticsearchTransport;
|
|
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
|
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
|
import co.elastic.clients.util.ContentType;
|
|
import co.elastic.clients.util.ContentType;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.http.Header;
|
|
import org.apache.http.HttpHeaders;
|
|
import org.apache.http.HttpHeaders;
|
|
import org.apache.http.HttpHost;
|
|
import org.apache.http.HttpHost;
|
|
import org.apache.http.HttpResponseInterceptor;
|
|
import org.apache.http.HttpResponseInterceptor;
|
|
|
|
+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.conn.ssl.TrustSelfSignedStrategy;
|
|
|
|
+import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
import org.apache.http.message.BasicHeader;
|
|
import org.apache.http.message.BasicHeader;
|
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
import org.elasticsearch.client.RestClient;
|
|
import org.elasticsearch.client.RestClient;
|
|
|
|
+import org.elasticsearch.client.RestClientBuilder;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
|
+import java.security.KeyManagementException;
|
|
|
|
+import java.security.KeyStoreException;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Locale;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
+@Slf4j
|
|
@Configuration
|
|
@Configuration
|
|
public class ElasticsearchConfig {
|
|
public class ElasticsearchConfig {
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
EsConfig config;
|
|
EsConfig config;
|
|
|
|
|
|
|
|
+
|
|
@Bean
|
|
@Bean
|
|
public ElasticsearchClient esClient() {
|
|
public ElasticsearchClient esClient() {
|
|
- RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort()))
|
|
|
|
- .setHttpClientConfigCallback(httpClientBuilder
|
|
|
|
- ->httpClientBuilder.setDefaultHeaders(
|
|
|
|
- List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
|
|
|
|
- .addInterceptorLast((HttpResponseInterceptor) (response, context)
|
|
|
|
- -> response.addHeader("X-Elastic-Product", "Elasticsearch"))).build();
|
|
|
|
|
|
+ SSLContext sslContext;
|
|
|
|
+ /*try {
|
|
|
|
+ sslContext =new SSLContextBuilder().loadTrustMaterial(null, TrustSelfSignedStrategy.INSTANCE)
|
|
|
|
+ .build();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }*/
|
|
|
|
+ //System.setProperty("java.security.krb5.conf",config.getKrb5());
|
|
|
|
+ //System.setProperty("java.security.auth.login.config", config.getJaas());
|
|
|
|
+ //System.setProperty("java.security.auth.login.defaultConfigurationName", "EsClient");
|
|
|
|
+ //System.setProperty("java.security.krb5.debug","true");
|
|
|
|
+ /*RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema()))
|
|
|
|
+ .setHttpClientConfigCallback(httpClientBuilder->
|
|
|
|
+ httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
|
|
|
|
+ .setSSLContext(sslContext)
|
|
|
|
+ .setDefaultCredentialsProvider(getCredentialsProvider(config.getUser(), config.getPassword()))
|
|
|
|
+ .setDefaultHeaders(List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
|
|
|
|
+ .addInterceptorLast((HttpResponseInterceptor) (response, context)-> response.addHeader("X-Elastic-Product", "Elasticsearch"))
|
|
|
|
+ ).build();*/
|
|
|
|
+
|
|
|
|
+ this.setSecConfig();
|
|
|
|
+ RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema()))
|
|
|
|
+ .setHttpClientConfigCallback(httpClientBuilder->
|
|
|
|
+ httpClientBuilder.setDefaultHeaders(List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
|
|
|
|
+ ).build();
|
|
|
|
+ SmRestClientBuilder builder= new SmRestClientBuilder(restClient.getNodes());
|
|
|
|
+ builder.setSslEnabled(true);
|
|
|
|
+ builder.setEsJaasConfFile(config.getJaas());
|
|
|
|
+ builder.authenticate(restClient);
|
|
|
|
+ //RestClient restClient = RestClient.builder(new HttpHost(config.getHosts(),config.getPort(),config.getSchema())).build();
|
|
ElasticsearchTransport transport = new RestClientTransport(
|
|
ElasticsearchTransport transport = new RestClientTransport(
|
|
restClient, new JacksonJsonpMapper());
|
|
restClient, new JacksonJsonpMapper());
|
|
ElasticsearchClient client = new ElasticsearchClient(transport);
|
|
ElasticsearchClient client = new ElasticsearchClient(transport);
|
|
|
|
+
|
|
|
|
+ //restClient.getHttpClient().
|
|
|
|
+ try {
|
|
|
|
+ // 查看指定索引
|
|
|
|
+ GetIndexResponse getIndexResponse = client.indices().get(s -> s.index("test4"));
|
|
|
|
+ Map<String, IndexState> result = getIndexResponse.result();
|
|
|
|
+ result.forEach((k, v) -> log.info("key = {},value = {}", k, v));
|
|
|
|
+ }catch (Exception ex){
|
|
|
|
+ log.info(ex.getMessage());
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ }
|
|
return client;
|
|
return client;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private void setSecConfig() {
|
|
|
|
+ try {
|
|
|
|
+ String userKeytabFile = config.getKeytab();
|
|
|
|
+ LoginUtil.setJaasFile(config.getUser(), userKeytabFile, config.getJaas());
|
|
|
|
+ LoginUtil.setKrb5Config(config.getKrb5());
|
|
|
|
+ System.setProperty("elasticsearch.kerberos.jaas.appname", "EsClient");
|
|
|
|
+ System.setProperty("es.security.indication", "true");
|
|
|
|
+ log.info(String.format(Locale.ENGLISH, "es.security.indication is %s.", System.getProperty("es.security.indication")));
|
|
|
|
+ } catch (Exception var3) {
|
|
|
|
+ Exception e = var3;
|
|
|
|
+ log.info("Failed to set security conf.", e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ private static CredentialsProvider getCredentialsProvider(String username, String password) {
|
|
|
|
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
+ credentialsProvider.setCredentials(AuthScope.ANY,
|
|
|
|
+ new UsernamePasswordCredentials(username, password));
|
|
|
|
+ return credentialsProvider;
|
|
|
|
+ }
|
|
}
|
|
}
|