ElasticSearch版本:7.7.1
配置
修改es的配置文件:elasticsearch.yml,添加如下配置
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
es的bin目錄下,執行設置用戶名和密碼的命令
./elasticsearch-setup-passwords interactive
這里會設置六個賬號的密碼:elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.

修改kibana的配置kibana.yml:

es修改密碼的命令如下:
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://192.168.140:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'
重啟es和kibana
訪問http://192.168.1.40:9200,需要輸入賬號密碼才可以訪問

同樣需要認證才可登陸,登陸賬號密碼為elastic/123456

java客戶端連接
需要與springdata es整合
RestHighLevelClient
@Bean
public RestHighLevelClient restHighLevelClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("192.168.1.40:9200")
.withConnectTimeout(Duration.ofSeconds(5))
.withSocketTimeout(Duration.ofSeconds(3))
.withBasicAuth("elastic", "123456")
.build();
return RestClients.create(clientConfiguration).rest();
}
SpringData ES配置
spring:
elasticsearch:
rest:
uris: "http://192.168.1.40:9200"
read-timeout: "10s"
username: "elastic"
password: "123456"
connection-timeout: "10s"
