ElasticSearch用户名密码添加及之后RestHighLevelClient客户端的使用方法

解决 elasticsearch-head 集群健康值: 未连接问题

# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在后面添加如下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User"

ElasticSearch 添加用户名密码

有文章说ElasticSearch的添加用户名和密码的插件是要收费的,但是ElasticSearch 7.3版本以上的就是免费的了,我没有去专门查询过,不过我的是7.3.1版本的,确实没有收费

1. 修改elasticsearch.yml

# 修改 elasticsearch-7.3.1\config\elasticsearch.yml
# 在后面添加如下内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
# 因为上面为了解决 elasticsearch-head 集群健康值: 未连接问题配置了这个属性
# 所以直接在原属性后添加本属性值就行了,修改后如下
# http.cors.allow-headers: "X-Requested-With, Content-Type, Content-Length, X-User, Authorization"

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

2. 修改password

此处我将所有密码都设置为了123456,大家自行设置即可

# 表示注释
# 先运行 elasticsearch.bat
# 在 elasticsearch-7.3.1\bin 目录下打开 cmd
# 执行指令:
elasticsearch-setup-passwords interactive
# 之后会出现
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,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
y
# 表示同意
# 之后依次输入以下的密码即可
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
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]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]

3. elasticsearch-head的使用

之后使用 elasticsearch-head 时需要这样使用

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

后面的 auth_password 跟的是上面设置的 elastic 的密码

4. kibana的使用

# 修改 config\kibana.yml
# 在后面添加如下语句即可
elasticsearch.username: "elastic"
elasticsearch.password: "123456"

5. RestHighLevelClient客户端的使用

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials("elastic", "123456"));  //es账号密码(默认用户名为elastic)
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost("localhost", 9200, "http"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                            httpClientBuilder.disableAuthCaching();
                            return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                        }
                    }));
  • 11
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
使用RestHighLevelClient连接带有用户名密码Elasticsearch机器,可以按照以下步骤进行操作: 1. 首先,确保你已经在Java项目中添加ElasticsearchJava客户端依赖,例如Maven或Gradle中添加以下依赖: ```xml <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.15.0</version> </dependency> ``` 2. 在代码中创建一个RestHighLevelClient实例,并设置连接参数,包括Elasticsearch的主机名、端口号、用户名密码。示例代码如下: ```java import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; public class ElasticsearchClientExample { public static void main(String[] args) { String hostname = "localhost"; // Elasticsearch主机名 int port = 9200; // Elasticsearch端口号 String username = "your_username"; // Elasticsearch用户名 String password = "your_password"; // Elasticsearch密码 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost(hostname, port, "http")) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder .setDefaultCredentialsProvider(new BasicCredentialsProvider() .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)))) ); // 使用client进行操作,例如执行搜索、索引等操作 // 关闭client连接 try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述示例代码中,需要将`hostname`、`port`、`username`和`password`替换为实际的Elasticsearch主机名、端口号、用户名密码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值