Elasticsearch之权限验证(Basic)

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

 

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

1
2
3
4
5
6
7
action.destructive_requires_name: true<br>
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 @   X-Wolf  阅读(6734)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
 
点击右上角即可分享
微信分享提示
杭州
18:04发布
杭州
18:04发布
4°
西南风
2级
空气质量
相对湿度
87%
今天
多云 / 小雨
4°/13°
周三
多云 / 晴
8°/19°
周四
多云
11°/24°