默认配置下,Elastic可以直接访问。有些时候需要配置成像数据库一样,经过用户名密码验证后,才能访问es中的索引数据。下面的方法可以实现,利用http-basic插件:
1.http-basic文件夹拷入plugins目录
2.elasticsearch.yml中加入:
http.basic.enabled: true
http.basic.user: "test"
http.basic.password: "test132"
http.basic.ipwhitelist: ["localhost", "127.0.0.1"]
http.basic.trusted_proxy_chains: []
http.basic.log: true
http.basic.xforward: "X-Forwarded-For"
3.获取client使用如下代码
/**
* 初始化es连接客户端
*@author ZRH
*/
public static void init() {
String clusterName = SystemParameter.get("esClusterName");
boolean clientTransportSniff = SystemParameter.getBoolean("esClientTransportSniff");
String[] hostPorts= SystemParameter.get("esHostPorts").split(",");
client = newClient(clusterName, clientTransportSniff, hostPorts);
String esUserName = SystemParameter.get("esUserName");//配置中为test
String esPassWord = SystemParameter.get("esPassWord");//配置中为test132
client.prepareGet().putHeader("Authorization","Basic " + encodeBasicHeader(esUserName,esPassWord));
System.out.println("my test:" + existsIndex(client,"123"));
}
4.有疑问加群:258143901