ElasticSearch配置需要权限认证的jest客户端
程序员文章站
2022-04-15 18:58:28
1.使用背景es服务端配置了xpack权限认证,客户端连接使用需要进行权限认证,之前写过的jestClient是不带权限认证的,实际上加了权限认证以后只是需要将es的用户名和密码放到【默认凭证】方法里面,其他没有大改动,二话不说,上代码:2.实际运用:application.yml配置文件配置es相关属性:elastic-search: enabled: true cluster-ips: 86.1.*.*,86.1.*.* cluster-rest-port: 9200 use...
1.使用背景
es服务端配置了xpack权限认证,客户端连接使用需要进行权限认证,之前写过的jestClient是不带权限认证的,实际上加了权限认证以后只是需要将es的用户名和密码放到【默认凭证】方法里面,其他没有大改动,二话不说,上代码:
2.实际运用:
- application.yml配置文件配置es相关属性:
elastic-search:
enabled: true
cluster-ips: 86.1.*.*,86.1.*.*
cluster-rest-port: 9200
username: zazz
password: *****
- 将配置文件中的键值对属性映射到ElasticSearchJestConfig配置类:
注意:xpack权限认证的,需要httpClientConfig设置默认凭证,defaultCredentials(username, password)
package com.wyk.studyhttp.es;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.strings.StringUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.ArrayList;
import java.util.List;
/**
* es 工具类 使用时 注入 JestClient
*/
@Configuration
@EnableAutoConfiguration
@ConfigurationProperties(prefix = "elastic-search")
@ConditionalOnProperty(prefix = "elastic-search", value = "enabled", havingValue = "true", matchIfMissing = false)
public class ElasticSearchJestConfig implements DisposableBean {
private String[] clusterIps;
private int clusterRestPort;
private String username;
private String password;
private String scheme = "http://";
private JestClient jestClient;
public ElasticSearchJestConfig() {
}
@Bean
public HttpClientConfig httpClientConfig() {
List<String> esIps = new ArrayList<String>();
HttpClientConfig httpClientConfig = null;
for (String ip : clusterIps) {
esIps.add(scheme + ip + ":" + clusterRestPort);
}
if (username != null && password != null) {
httpClientConfig = new HttpClientConfig.Builder(esIps)
.defaultCredentials(username, password)//设置默认凭证(es的用户名和密码),否则es报权限异常
.multiThreaded(true).build();
return httpClientConfig;
} else {
httpClientConfig = new HttpClientConfig.Builder(esIps).multiThreaded(true).build();
return httpClientConfig;
}
}
@Bean
public JestClient jestClient() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(httpClientConfig());
return factory.getObject();
}
public String[] getClusterIps() {
return clusterIps;
}
public void setClusterIps(String[] clusterIps) {
this.clusterIps = clusterIps;
}
public int getClusterRestPort() {
return clusterRestPort;
}
public void setClusterRestPort(int clusterRestPort) {
this.clusterRestPort = clusterRestPort;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getScheme() {
return scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
@Override
public void destroy() throws Exception {
if (jestClient != null) {
jestClient.shutdownClient();
}
}
}
- jestClient()方法返回一个JestClient对象,使用时注入JestClient即可。
完毕!
本文地址:https://blog.csdn.net/wyk_dao/article/details/109640143