【Elasticsearch】如何給ES設置必須以用戶名密碼訪問


【本文涉及ES版本】

7.11.1

【elasticsearch.yml中設置】

增加以下兩項設置:

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

【啟動es后使用elasticsearch-setup-passwords進行密碼設置】

在完成修改elasticsearch.yml后,先啟動es;

之后再打開一個終端如putty,以同一用戶登錄,進入es的bin目錄,敲入命令:./elasticsearch-setup-passwords interactive

之后分別給elastic, kibana, logstash_system,beats_system等用戶設置密碼,具體如下:

[hy@localhost bin]$ ./elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,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


Enter password for [elastic]:
passwords must be at least [6] characters long
Try again.
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
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_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]

執行完畢之后,訪問es節點就需要用戶名密碼了,如下:

如果密碼不對就會輸出:

{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}

輸對了密碼才會有正常的:

{
  "name" : "node-1",
  "cluster_name" : "liangshan",
  "cluster_uuid" : "h6AscLaBS0qyOKdfq1DO8w",
  "version" : {
    "number" : "7.11.1",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "ff17057114c2199c9c1bbecc727003a907c0db7a",
    "build_date" : "2021-02-15T13:44:09.394032Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

加了密碼后,用RestTemplate訪問es的方式也需要進行調整. 可以參考:https://www.cnblogs.com/heyang78/p/15945626.html

 

END

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM