ElasticSearch用戶名密碼添加及之後RestHighLevelClient客戶端的使用方法

解決 elasticsearch-head 集羣健康值: 未連接問題

# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在後面添加如下內容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"

ElasticSearch 添加用戶名密碼

有文章說ElasticSearch的添加用戶名和密碼的插件是要收費的,但是ElasticSearch 7.3版本以上的就是免費的了,我沒有去專門查詢過,不過我的是7.3.1版本的,確實沒有收費

1. 修改elasticsearch.yml

# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在後面添加如下內容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 因爲上面爲了解決 elasticsearch-head 集羣健康值: 未連接問題配置了這個屬性
# 所以直接在原屬性後添加本屬性值就行了,修改後如下
# http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User, Authorization"

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

2. 修改password

此處我將所有密碼都設置爲了123456,大家自行設置即可

# 表示註釋
# 先運行 elasticsearch.bat
# 在 elasticsearch-7.3.1\bin 目錄下打開 cmd
# 執行指令:
elasticsearch-setup-passwords interactive
# 之後會出現
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]
# 按下 y
y
# 表示同意
# 之後依次輸入以下的密碼即可
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]

3. elasticsearch-head的使用

之後使用 elasticsearch-head 時需要這樣使用

http://localhost:9100/?auth_user=elastic&auth_password=123456

後面的 auth_password 跟的是上面設置的 elastic 的密碼

4. kibana的使用

# 修改 config\kibana.yml
# 在後面添加如下語句即可
elasticsearch.username: "elastic"
elasticsearch.password: "123456"

5. RestHighLevelClient客戶端的使用

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("elastic", "123456"));  //es賬號密碼(默認用戶名爲elastic)
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("localhost", 9200, "http"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            httpClientBuilder.disableAuthCaching();
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                        }
                    }));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章