最近有个需求,为保证数据的安全性,需要给es添加密码,所以就整了一下,因为环境不同,分为两种,一种是仅设置密码,没有秘钥证书的,另外一种是设置密码也设置了秘钥证书

        本次记录的是第一种方式仅设置密码方式,如需要证书秘钥方式的,可以点我这篇博客elasticsearch6.81设置证书秘钥xpack连接springboot集成

一、安装es并设置密码

1.百度现成的挺多就不再详细记录了,可以自行去查找

不过也推荐一个 https://www.cnblogs.com/weibanggang/p/11589464.html (写的相当不错)

2.安装完成后设置密码

改elasticsearch.yml配置文件打开xpack安全认证,并重启Elasticsearch,添加内容如下:

xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

重启后,在Elasticsearch服务目录下执行下面命令,初始化认证密码,第一个密码设置务必记住

./bin/elasticsearch-setup-passwords interactive

用浏览器访问9200,根据提示输入认证用户名和密码(第一个)即可查看Elasticsearch信息;

如果使用kibana,需要kibana.yml配置文件中添加如下几行:

elasticsearch.username: "kibana"
elasticsearch.password: "123456"   #初始化设置的密码

截止到此 已经安装好es并设置了密码

------------------------------------------------------- 分割线------------------------------------------------------- 

二、使用springboot集成es加密

es安装配置文件

1.完成上述密码设置后,修改es的配置文件 /config/elasticsearch.yml 完事记得重启

xpack.security.enabled: true
#xpack.security.transport.ssl.enabled: true
discovery.type: single-node
action.destructive_requires_name: true

springboot版本

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> 
    </parent>

pom文件

 <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>6.8.10</elasticsearch.version>
    </properties>  

<!--es -->
        <!-- Elasticsearch相关配置开始 -->
        <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>

yml文件

注意在根目录  没在spring下边
elasticsearch:
  cluster-nodes: xx.xx.xx.xxx(服务器地址)
  cluster-name: 对应你的cluster-name
  cluster-password: elastic:elastic(es设置好的账号密码,格式账号:密码)

java代码集成 注入elasticsearchTemplate

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.net.InetAddress;

@Slf4j
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
public class ElasticsearchConfig {

    private String clusterName;
    private String clusterNodes;
    private String clusterPassword;

    @Bean
    public Client client() throws Exception {
        Settings esSettings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("xpack.security.user", clusterPassword)
                .put("xpack.security.transport.ssl.enabled", false)
                //增加嗅探机制,找到ES集群,非集群置为false
                .put("client.transport.sniff", false)
                //增加线程池个数
                .put("thread_pool.search.size", 20)
                .build();
        return new PreBuiltXPackTransportClient(esSettings).addTransportAddress(new TransportAddress(InetAddress.getByName(clusterNodes), 9300));
    }

    @Bean(name = "elasticsearchTemplate")
    public ElasticsearchOperations elasticsearchTemplateCustom() throws Exception {
        ElasticsearchTemplate elasticsearchTemplate;
        try {
            elasticsearchTemplate = new ElasticsearchTemplate(client());
            log.info("初始化ElasticsearchTemplate成功");
            return elasticsearchTemplate;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("初始化ElasticsearchTemplate失败");
            return new ElasticsearchTemplate(client());
        }
    }

}

 配置完成即可使用 ElasticsearchRepository或者ElasticsearchTemplate方式任意都可

ElasticsearchRepository示例如下:

import cn.cnic.instdb.model.resources.ResourcesManageEs;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ResourcesManageEsRepository extends ElasticsearchRepository<ResourcesManageEs, String> {

}
    @Resource
    private ResourcesManageEsRepository resourcesManageEsRepository;

    //插入
    @Test
    public void test() {
        ResourcesManageEs ResourcesManageEs = new ResourcesManageEs();
        ResourcesManageEs.setTitleZh("test");
        ResourcesManageEs.setCstr("test");
        ResourcesManageEs.setId(UUID.randomUUID().toString().replace("-", ""));
        resourcesManageEsRepository.save(ResourcesManageEs);
    }

    //删除
    @Test
    public void deleteAllEs(){
        resourcesManageEsRepository.deleteAll();
    }

总结

针对elasticsearch加密认证集成就这么简单,亲测已经成功使用中,如果有问题欢迎留言讨论~

如果对你有所帮助,别忘了点赞让我知道。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐