在《你竟然还去服务器上捞日志,搭个日志收集零碎难道不香么!》一文中咱们介绍过ELK日志收集零碎的搭建,因为咱们的Kibana没有任何平安爱护机制,如果部署到公网上去的话,任何人都能够查看你的日志了。日志裸露在网络上可不是件好事件,明天教大家如何给Kibana设置登录认证来爱护它。

SpringBoot实战电商我的项目mall(40k+star)地址:https://github.com/macrozheng/mall

实现原理

因为Kibana的日志信息都存储在Elasticsearch中,所以只有给Elasticsearch开启X-PACK中的平安性能,并给预置的账号设置好明码即可。Elasticsearch设置好之后,就能够在Kibana中对用户、角色、权限进行治理了,本文应用的ELK组件版本均为7.6.2

Elasticsearch设置明码

  • 批改Elasticsearch的配置文件并开启X-PACK中的平安性能,该配置文件在装置目录的config文件夹上面,例如elasticsearch-7.6.2\config\elasticsearch.yml
http.cors.enabled: truehttp.cors.allow-origin: "*"http.cors.allow-headers: Authorizationxpack.security.enabled: truexpack.security.transport.ssl.enabled: true
  • 启动Elasticsearch服务,启动命令在bin目录下,例如elasticsearch-7.6.2\bin\elasticsearch.bat
  • bin目录下应用如下命令elasticsearch-setup-passwords interactive批改预置账号的明码,期间须要设置多个账号密码,我都设置成了123456

  • 期间设置了好几个账号,咱们先来理解下这些账号都有啥作用吧;
elastic:超级管理员账号kibana:Kibana拜访专用账号logstash_system:Logstash拜访专用账号beats_system:FileBeat拜访专用账号apm_system:APM零碎专用账号remote_monitoring_user:近程监控账号
  • 接下来咱们须要在Kibana的配置文件中增加能够拜访Elasticsearch的账号,该配置文件在装置目录的config文件夹上面,例如kibana-7.6.2\config\kibana.yml
elasticsearch.username: "kibana"elasticsearch.password: "123456"
  • 启动Kibana服务,启动命令在bin目录下,例如kibana-7.6.2\bin\kibana.bat
  • 当Kibana启动实现后,咱们拜访的时就须要登录认证了,应用超级管理员账号elastic:123456能够进行登录,拜访地址:http://localhost:5601

  • 登录胜利后,在咱们的Management选项中能够找到平安相干的配置,在此咱们能够对用户、角色、权限进行设置。

SpringBoot平安拜访

因为Elasticsearch开启X-PACK中的平安性能,当咱们的SpringBoot利用拜访Elasticsearch时,也须要设置用户名和明码了!
  • 咱们能够间接在SpringBoot中设置超级管理员账号,但这不是个好方法,咱们还是本人建个角色和账号吧!
  • 首先在Kibana中创立一个利用拜访专用的角色app_user

  • 创立一个用户并配置好该角色,账号密码为app:123456

  • 批改SpringBoot利用的配置文件application.yml,配置好账号密码即可失常拜访了!
spring:  elasticsearch:    rest:      uris: http://localhost:9200      username: app      password: 123456

Logstash平安拜访

因为Elasticsearch开启X-PACK中的平安性能,向Elasticsearch输入日志的Logstash也须要设置用户名和明码了!
  • 首先批改咱们原来的Logstash配置文件logstash.conf,在output节点下设置拜访Elasticsearch的用户名和明码,间接应用咱们创立的app:123456账号即可;
input {  tcp {    mode => "server"    host => "0.0.0.0"    port => 4560    codec => json_lines    type => "debug"  }  tcp {    mode => "server"    host => "0.0.0.0"    port => 4561    codec => json_lines    type => "error"  }  tcp {    mode => "server"    host => "0.0.0.0"    port => 4562    codec => json_lines    type => "business"  }  tcp {    mode => "server"    host => "0.0.0.0"    port => 4563    codec => json_lines    type => "record"  }}filter{  if [type] == "record" {    mutate {      remove_field => "port"      remove_field => "host"      remove_field => "@version"    }    json {      source => "message"      remove_field => ["message"]    }  }}output {  elasticsearch {    hosts => ["localhost:9200"]    action => "index"    codec => json    index => "mall-tiny-%{type}-%{+YYYY.MM.dd}"    template_name => "mall-tiny"    user => app    password => "123456"  }}
  • 应用指定配置文件启动Logstash服务,启动命令在bin目录下,例如logstash-7.6.2\bin\logstash.bat
logstash -f logstash.conf
  • 接下来在Kibana中就能够查看到利用输入的日志了!

我的项目源码地址

https://github.com/macrozheng...

本文 GitHub https://github.com/macrozheng/mall-learning 曾经收录,欢送大家Star!