ElasticSearch 使用X-Pack加密后,Java 连接

本文介绍了一家公司因未设置ES密码导致遭受勒索病毒攻击的问题,详细阐述了使用X-Pack安全工具进行加密的过程。包括引入POM文件依赖、新建ElasticsearchConfig类以及在application.yml中设置相关参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因公司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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值