当前位置: 首页 > news >正文

Java | 基于Kerberos认证对接华为云Elasticsearch

可以通过华为官方提供的Java客户端,来实现基于Kerberos认证访问和操作华为云Elasticsearch;亦可以使用更加通用的开源Elasticsearch Java客户端bboss,来实现基于Kerberos认证访问和操作华为云Elasticsearch。

本文介绍使用bboss实现基于Kerberos认证访问和操作华为云Elasticsearch的方法。

1. bboss介绍

bboss是一个高性能高兼容性的Elasticsearch java客户端框架:
在这里插入图片描述
更多bboss介绍,可以访问文档了解:https://esdoc.bbossgroups.com/#/README

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验证数据源:

		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

相关文章:

  • TFChat:腾讯大模型知识引擎+飞书机器人实现AI智能助手
  • Python Spider-dy实时弹幕监听与记录系统的实现
  • SEO炼金术(4)| Next.js SEO 全攻略
  • Springboot基础篇(3):Bean管理
  • 如何在netlify一键部署静态网站
  • 【C++】:STL详解 —— list类
  • mapbox基础,加载background背景图层
  • 模拟算法.
  • 核桃派开发板的vnc viewer连接
  • 京东云鼎消息队列订阅详细步骤(已完成:order_order_finish)
  • ERP项目实施流程及存在的风险
  • 机器学习介绍与数据集
  • Amazon Outposts:构建混合云的安全堡垒,让数据安全“零距离”
  • Python基于机器学习的微博舆情情感分析系统,微博评论情感分析可视化系统(全新升级)
  • Redis 之持久化机制(The Persistence Mechanism of Redis)
  • 字符串_ 反转字符串II
  • 【学写LibreCAD】1 创建核心模块库
  • 数据解析与处理
  • 我的AI工具箱Tauri版-InteriorDecorationDesignDrawing平面设计图生成房屋所有室内的效果图
  • SGMII(Serial Gigabit Media Independent Interface)详解
  • 让“五颜六色”面孔讲述上海故事,2025年上海城市推荐官开启选拔
  • 印巴战火LIVE丨“快速接近战争状态”?印度袭击巴军事基地,巴启动反制军事行动
  • 央行最新报告:积极落地5月推出的一揽子金融政策,促进经济供需平衡、物价合理回升
  • 碧桂园境外债务重组:相当于现有公众票据本金额逾50%的持有人已加入协议
  • 普雷沃斯特当选新一任天主教罗马教皇
  • 迪拜金融市场CEO:2024年市场表现出色,超八成新投资者来自海外