Elasticsearch 7.x配置用户名密码访问 开启x-pack验证

一、修改elasticsearch 配置文件

  1.在配置文件中开启x-pack验证

#进入es安装目录下的config目录
vim elasticsearch.yml

# 配置X-Pack
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

  2.重启elasticsearch服务

  3.执行设置用户名和密码的命令,需要为4个用户分别设置密码:elastic、kibana、logstash_system、beats_system

#进入bin目录
cd /usr/local/elasticsearch-7.2.0/bin

#执行命令
./elasticsearch-setup-passwords interactive

 二、修改kibana配置文件

  1.修改kibana.yml配置文件,添加以下配置

#进入kibana安装目录
cd /usr/local/kibana-7.2.0-linux-x86_64/config

#修改配置文件
vim kibana.yml

#添加配置
elasticsearch.username: "elastic"
elasticsearch.password: "xxx"

  2.重启kibana服务

  3.登录kibana

  

三、Springboot 配置

  1.SpringBoot2.2.x版本才支持ElasticSearch7.x,需要先升级SpringBoot版本

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

  2.修改application.yml

spring:
  elasticsearch:
    #es配置
    rest:
      #最新配置方式使用restful风格,端口从9300 -> 9200
      uris: xx.xx.xx.xx:9200
      username: elastic
      password: xxx    

  3.补充

#原来spring-boot-starter-data-elasticsearch连接ES主要使用ElasticsearchTemplate进行操作
#新版本主要使用ElasticsearchRestTemplate

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

 补充扩展

  Linux安装ElasticSearch7.X

  搭建Elasticsearch可视化界面 Kibana

原文地址:https://www.cnblogs.com/xhq1024/p/12844654.html