因公司ES未设密码,被植入勒索病毒,故采用X-Pack安全工具加密。
一、引入 POM 文件
<properties>
<elasticsearch.version>6.5.4</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>elasticsearch-releases</id>
<url>https://artifacts.elastic.co/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
二、新建 ElasticsearchConfig 类
package com.demo.config;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
@EnableElasticsearchRepositories
public class ElasticsearchConfig {
@Value("${es.cluster.name:#{null}}")
private String name;
@Value("${es.user:#{null}}")
private String user;
@Value("${es.password:#{null}}")
private String password;
@Value("${es.url:#{null}}")
private String url;
@Value("${es.port:#{null}}")
private String port;
@Value("${es.http.ssl.keystore.password:#{null}}")
private String httpKeystorePassword;
@Value("${es.http.ssl.truststore.password:#{null}}")
private String httpTruststorePassword;
@Value("${es.http.ssl.enabled:#{null}}")
private String httpSslEnable;
@Value("${es.transport.ssl.keystore.password:#{null}}")
private String keystorePassword;
@Value("${es.transport.ssl.truststore.password:#{null}}")
private String truststorePassword;
@Value("${es.transport.ssl.enabled:#{null}}")
private String transportSslEnabled;
@Value("${es.transport.ssl.verification-mode:#{null}}")
private String transportVerificationMode;
@Value("${es.certificates-path:#{null}}")
private String certificatesPath;
@Bean
public TransportClient transportClient() throws UnknownHostException {
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put("cluster.name", name)
.put("xpack.security.user", user+":"+password)
.put("xpack.security.transport.ssl.keystore.password", keystorePassword)
.put("xpack.security.transport.ssl.truststore.password", truststorePassword)
.put("xpack.security.transport.ssl.enabled", transportSslEnabled)
.put("xpack.security.transport.ssl.verification_mode", transportVerificationMode)
.put("xpack.security.http.ssl.keystore.password", httpKeystorePassword)
.put("xpack.security.http.ssl.truststore.password", httpTruststorePassword)
.put("xpack.security.http.ssl.enabled", httpSslEnable)
.put("xpack.security.transport.ssl.keystore.path", certificatesPath+"/elastic-certificates.p12")
.put("xpack.security.transport.ssl.truststore.path", certificatesPath+"/elastic-certificates.p12")
.put("xpack.security.http.ssl.keystore.path", certificatesPath+"/elastic-certificates.p12")
.put("xpack.security.http.ssl.truststore.path", certificatesPath+"/elastic-certificates.p12")
.build())
.addTransportAddress(new TransportAddress(InetAddress.getByName(url), Integer.valueOf(port)));
return client;
}
@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws Exception{
return new ElasticsearchTemplate(transportClient());
}
}
三、设置application.yml
es:
cluster:
name: elasticsearch
user: elastic
password: elastic
url: localhost
port: 9300
certificates-path: C:\certificates #证书路径
http:
ssl:
enabled: true
keystore:
password: 123456
truststore:
password: 123456
transport:
ssl:
verification-mode: certificate
enabled: true
keystore:
password: 123456
truststore:
password: 123456