可以通过华为官方提供的Java客户端,来实现基于Kerberos认证访问和操作华为云Elasticsearch;亦可以使用更加通用的开源Elasticsearch Java客户端bboss,来实现基于Kerberos认证访问和操作华为云Elasticsearch。
本文介绍使用bboss实现基于Kerberos认证访问和操作华为云Elasticsearch的方法。
bboss_3">1. bboss介绍
bboss是一个高性能高兼容性的Elasticsearch java客户端框架:
更多bboss介绍,可以访问文档了解:https://esdoc.bbossgroups.com/#/README
bboss_7">2. 集成bboss
集成bboss非常简单,只需在项目中导入bboss对应的maven坐标即可:
<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-datatran-jdbc</artifactId>
<version>7.3.5</version>
</dependency>
实际bboss版本号可以参考文档获取:
https://esdoc.bbossgroups.com/#/changelog
3. 配置和使用Elasticsearch数据源
在项目中导入bboss maven坐标后,定义基于Kerberos认证的Elasticsearch数据源以及通过ClientInterface验证数据源:
java"> Map properties = new HashMap();
/**
* 配置Elasticsearch数据源参数,这里只设置必须的配置项,更多配置参考文件:
* https://gitee.com/bboss/elasticsearchdemo/blob/master/src/main/resources/application.properties
*/
//定义Elasticsearch数据源名称:esDS,后续通过esDS获取对应数据源的客户端API操作和访问Elasticsearch
properties.put("elasticsearch.serverNames","esDS");
//es服务器地址和端口,多个用逗号分隔
properties.put("esDS.elasticsearch.rest.hostNames","192.168.137.1:8200");
//是否在控制台打印dsl语句,log4j组件日志级别为INFO或者DEBUG
properties.put("esDS.elasticsearch.showTemplate","true");
//集群节点自动发现,关闭服务发现机制
properties.put("esDS.elasticsearch.discoverHost","false");
//Kerberos安全认证配置--开始
properties.put("esDS.http.kerberos.serverRealmPath","/elasticsearch/serverrealm");//配置华为云Elasticsearch服务端Princpal查询服务地址
properties.put("esDS.http.kerberos.useSubjectCredsOnly","false");
//华为云Elasticsearch krb5.conf文件,由华为提供
properties.put("esDS.http.kerberos.krb5Location","C:/environment/es/8.13.2/elasticsearch-8.13.2/config/krb5.conf");
//华为云Elasticsearch jaas.conf文件,由华为提供
properties.put("esDS.http.kerberos.loginConfig","C:/environment/es/8.13.2/elasticsearch-8.13.2/config/jaas.conf");
//配置登录模块名称,与华为云Elasticsearch jaas.conf文件中的模块名称一致
properties.put("esDS.http.kerberos.loginContextName","ESClient");
//配置是否debug Kerberos认证详细日志
properties.put("esDS.http.kerberos.debug","true");
//Kerberos安全认证配置--结束
//启动和初始化Elasticsearch数据源
ElasticSearchBoot.boot(properties);
//通过Elasticsearch数据源名称esDS获取对应数据源的客户端API,操作和访问Elasticsearch
//可以反复根据数据源名称esDS,调用下面的方法获取ClientInterface接口实例,始终返回单实例多线程安全的ClientInterface对象
ClientInterface clientInterface = ElasticSearchHelper.getRestClientUtil("esDS");
//验证客户端:通过Elasticsearch rest服务获取ES集群信息
String result = clientInterface.executeHttp("/?pretty", ClientInterface.HTTP_GET);
logger.info(result);
//验证客户端:通过API获取ES集群配置参数
logger.info(clientInterface.getClusterSettings());
//验证客户端:通过API判断索引demo是否存在
boolean exist = clientInterface.existIndice("demo");
logger.info(exist+"");
//验证客户端:通过API从索引demo获取文档id为1的文档数据(String报文)
String doc = clientInterface.getDocument("demo","1");
logger.info(doc+"");
//验证客户端:通过API从索引demo获取文档id为1的文档数据(or mapping示例:返回Map结构的数据,亦可以转换为PO对象)
Map mapdoc = clientInterface.getDocument("demo","1",Map.class);
基于配置Kerberos认证实现代码非常简洁,只需在平常数据源参数配置的基础上,增加Kerberos认证相关的参数即可。上述代码中涉及的华为云Kerberos配置文件krb5.conf和jaas.conf,由华为云Elasticsearch提供,这里不单独介绍,需要注意一下:http.kerberos.loginContextName参数对应的值需与jaas.conf配置文件中认证模块名称一致,这里是ESClient。
下面是一个jaas.conf配置内容样例:
ESClient {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="C:/environment/es/8.13.2/elasticsearch-8.13.2/config/elastic.keytab"
principal="elastic/admin@BBOSSGROUPS.COM"
useTicketCache=false
storeKey=true
debug=false;
};
其中的elastic.keytab文件由华为云Elasticsearch提供即可。更多ClientInterface api使用方法,可以访问下面参考资料中提供的链接了解。
本文对应的代码源码工程下载地址:
码云 https://gitee.com/bboss/eshelloword-booter
Github https://github.com/bbossgroups/eshelloword-booter
对应的Kerberos认证Java Demo CustormInitAndBootKerberosAuth.java
4. 参考资料
Elasticsearch文档增删改查操作介绍 https://esdoc.bbossgroups.com/#/document-crud
高性能elasticsearch ORM开发库使用介绍 https://esdoc.bbossgroups.com/#/development
快速开始bboss https://esdoc.bbossgroups.com/#/quickstart
开发交流 https://esdoc.bbossgroups.com/#/supportus