elasticsearch 7.6.1 的用户名和密码,kibana修改密码

一/设置用户名密码

版本elasticsearch 7.6.1

elasticsearch/config/elasticsearch.yaml文件中添加如下内容
# ---------------------------------- Auth -------------------------------------

xpack.security.enabled: true

xpack.license.self_generated.type: basic

xpack.security.transport.ssl.enabled: true

在elasticsearch bin目录下使用如下命令
./elasticsearch-setup-passwords interactive

设置密码,必须大于6位
elastic: admin1
apm_system: admin1
kibana: admin1
logstash_system: admin1
beats_system: admin1
remote_monitoring_user: admin1

进入kibana/config/kibana.yaml文件中修改
elasticsearch.username = elastic
elasticsearch.password = admin1

二/java代码访问

java代码中,配置一个config,使用如下代码,可以全局配置RestHighLevelClient,也可以RestHighLevelClient,执行crud时来进行设置.

import cn.hutool.core.codec.Base64;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticClientConfig {
    @Value("${elasticsearch.clientIps}")
    private String clientIps;

    @Value("${elasticsearch.httpPort}")
    private int httpPort;

    @Value("${elasticsearch.username}")
    private String username;

    @Value("${elasticsearch.password}")
    private String password;

    @Value("${elasticsearch.heapSize}")
    private Integer heapSize;

    public static  RequestOptions COMMON_OPTIONS=null;

    void initRequestOptions (){
        String TOKEN = Base64.encode(username+":"+password) ;
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        builder.addHeader("Authorization","Basic "+TOKEN);
        builder.setHttpAsyncResponseConsumerFactory(
                new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(heapSize*1024*1024));
        COMMON_OPTIONS = builder.build();
    }
    private HttpHost[] getHttpHosts(String clientIps, int esHttpPort) {

        String[] clientIpList = clientIps.split(",");
        HttpHost[] httpHosts = new HttpHost[clientIpList.length];
        for (int i = 0; i < clientIpList.length; i++) {
            httpHosts[i] = new HttpHost(clientIpList[i], esHttpPort, "http");
        }
        return httpHosts;
    } 

    /**
     * 创建带HTTP Basic Auth认证rest客户端
     */
    @Bean
    public RestHighLevelClient restHighLevelClient() {
        initRequestOptions ();
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort))
                .setHttpClientConfigCallback(
                        (HttpAsyncClientBuilder httpAsyncClientBuilder) ->
                                httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                                );
    }

    //不带用户名密码验证
//    @Bean
    public RestHighLevelClient restClient() {
        initRequestOptions ();
        return new RestHighLevelClient(RestClient.builder(getHttpHosts(clientIps, httpPort)));
    }
}

 

 配置完以后使用elastic用户登录kibana,并在左侧能看到安全性这个选项,其中定义用户和角色可以对用户和角色做修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值