Elasticsearch之权限验证(Basic)

  目前使用的Es版本为7.5版本, 在7.X版本中已经可以免费的使用x-pack进行用户验证了

 

1. 修改elasticsearch主节点配置文件:

action.destructive_requires_name: true
http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type xpack.security.enabled: true xpack.license.self_generated.type: basic xpack.security.transport.ssl.enabled: true

 

action.destructive_requires_name: true: 设置之后只限于使用特定名称来删除索引,使用_all 或者通配符来删除索引无效(上述中说明配置文件中禁止后此方式不能使用)】

注意:上面对于跨域的配置,下面使用elasticsearch-head进行连接的时候会用到   使用到的是基础验证类型

 

2. 创建身份认证

#系统自动生成密码
./bin/elasticsearch-setup-passwords auto

#自定义密码
./bin/elasticsearch-setup-passwords interactive

 

3. 重启elasticsearch服务

systemctl restart elasticsearch

 


 

Kibana配置:

elasticsearch.username: "elastic"
elasticsearch.password: "具体密码"

 

 


 

配置head插件

1. 安装

  GitHub地址:https://github.com/zhaoyunxing92/elasticsearch-head.git

  具体安装可以看说明,这里简单说一下:

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
open http://localhost:9100/

 

  打开服务器ip地址,访问即可: http://59.132.16.217:9100

  

 

   可以修改_site/app.js 大概4374行左右,指定默认访问的集群地址

app.App = ui.AbstractWidget.extend({
4368                 defaults: {
4369                         base_uri: null
4370                 },
4371                 init: function(parent) {
4372                         this._super();
4373                         this.prefs = services.Preferences.instance();
4374                         this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://59.132.16.127:9200";
4375                         if( this.base_uri.charAt( this.base_uri.length - 1 ) !== "/" ) {
4376                                 // XHR request fails if the URL is not ending with a "/"
4377                                 this.base_uri += "/";
4378                         }
4379                         if( this.config.auth_user ) {
4380                                 var credentials = window.btoa( this.config.auth_user + ":" + this.config.auth_passwo     rd );                      

 

 

2.配置

  elasticsearch安装x-pack插件之后,head插件就无法使用了,因为x-pack中加入了安全模块(security机制),这个时候需要在elasticseach.yml中再增加下面一行配置即可解决

http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type


上面提到的内容

 

  这样访问页面的时候,就需要带上用户名和密码进行访问

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

# 指定用户名和密码
auth_user
auth_password

 

 


 

现在已经添加了验证, 这个时候, 如果我们不想暴露自己的端口,可以使用nginx进行一次转发,对外只提供80端口

server {
        listen       80;
        server_name  es.*.com; #公网域名地址

        location / {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/conf/htpasswd;
            proxy_pass http://localhost:9100;
            proxy_set_header Host $host:9100;
            proxy_set_header X-Real-Ip $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Via "nginx";
        }

}

具体账号密码配置,可参考:nginx之访问控制

 

我们来禁用9100端口,只在127.0.0.01:9100,配合nginx使用即可

elasticsearch-head/Gruntfile.js

在connect –>server–>options下添加hostname:’127.0.0.1’ 允许本地ip可以访问

connect: {
                        server: {
                                options: {
                                        hostname: '127.0.0.1',
                                        port: 9100,
                                        base: '.',
                                        keepalive: true
                                }
                        }
                }

 

添加开机启动:

vim  /etc/rc.local

/usr/local/elasticsearch-head/node_modules/grunt/bin/grunt server &

 

posted @ 2020-03-25 11:04  X-Wolf  阅读(6440)  评论(0编辑  收藏  举报