kafka+Kraft模式集群+安全认证

news/2025/2/12 7:50:42/

Kraft模式安全认证

前章内容聊到了Kafka的Kraft集群的配置及使用。本篇再来说说kafka的安全认证方面的配置,。

Kafka提供了多种方式来进行安全认证,包括身份认证、授权和加密传输。一些常用的Kafka安全认证方式:

  1. SSL/TLS:使用SSL/TLS协议来加密Kafka与客户端之间的通信,保证数据的机密性和完整性。可以通过配置Kafka的SSL证书、密钥和信任的CA证书来启用SSL/TLS。客户端也需要使用相应的证书与Kafka进行通信。
  2. SASL(Simple Authentication and Security Layer):使用SASL进行身份认证。Kafka支持多种SASL机制,如PLAIN、GSSAPI等。可以通过配置Kafka的SASL机制和用户凭证(用户名和密码、密钥等)来启用SASL身份认证。
  3. ACL(Access Control List):使用ACL进行授权管理。ACL允许你配置哪些用户或组可以访问Kafka的哪些主题和分区,并对其进行读取或写入权限的控制。ACL的配置可以在Kafka的配置文件中进行。

这些安全认证方式可以单独使用,也可以组合使用,以实现更高级别的安全性。为了配置Kafka的安全认证,需要对Kafka和客户端进行相应的配置,并生成所需的证书和凭证。

本文针对SASL进行身份认证

开始配置

服务器数量有限,暂时使用单机部署kafka集群,此文给予配置参考,实际还是要按项目的真实情况去处理了。

准备3个kafka,分别是kafka01kafka02kafka03,分别到它们的config/kraft/server.properties中做配置:

kafka01的server.properties

process.roles=broker,controller
node.id=1
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
listeners=SASL_PLAINTEXT://:19092,CONTROLLER://:19093
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
inter.broker.listener.name=SASL_PLAINTEXT
advertised.listeners=SASL_PLAINTEXT://192.168.8.122:19092
controller.listener.names=CONTROLLER
log.dirs=/wlh/kafka01/data

kafka02

process.roles=broker,controller
node.id=2
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
listeners=SASL_PLAINTEXT://:29092,CONTROLLER://:29093
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
inter.broker.listener.name=SASL_PLAINTEXT
advertised.listeners=SASL_PLAINTEXT://192.168.8.122:29092
controller.listener.names=CONTROLLER
log.dirs=/wlh/kafka02/data

kafka03

process.roles=broker,controller
node.id=3
controller.quorum.voters=1@localhost:19093,2@localhost:29093,3@localhost:39093
listeners=SASL_PLAINTEXT://:39092,CONTROLLER://:39093
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
inter.broker.listener.name=SASL_PLAINTEXT
advertised.listeners=SASL_PLAINTEXT://192.168.8.122:39092
controller.listener.names=CONTROLLER
log.dirs=/wlh/kafka03/data

先确保你的kafka的数据目录是空的,执行下删除(后面初始化时会自动创建目录)

rm -rf /wlh/kafka01/data /wlh/kafka02/data /wlh/kafka03/data

创建一个kafka sasl认证的服务配置

可以在kafka的config目录下新建一个kafka_server_jaas.conf文件,然后认证信息写好:

KafkaServer {org.apache.kafka.common.security.plain.PlainLoginModule requiredserviceName="kafka"username="admin"password="eystar8888"user_kafka="kafka1234";
};

上面的配置中声明了管理员为admin,密码是eystar8888,并且声明了一个用户名为kafka,密码是kafka1234的用户,客户端连接时使用用户为kafka可以成功进行认证。

而需要注意的是:上面的配置中的分号;,不能少,否则就掉坑里了。

配置kafka服务的启动脚本

上面设置好sasl认证的配置后,我们需要在kafka启动的服务脚本中,将此配置加入进去。

可以直接修改bin/kafka-server-start.sh,亦或者拷贝一份kafka-server-start.sh命名为kafka-server-start-saal.sh(名称自定义即可)

在这里插入图片描述

export KAFKA_OPTS="-Djava.security.auth.login.config=/wlh/kafka01/config/kafka_server_jaas.conf"

kafka02和kafka03同样这样配置好

export KAFKA_OPTS="-Djava.security.auth.login.config=/wlh/kafka02/config/kafka_server_jaas.conf"
export KAFKA_OPTS="-Djava.security.auth.login.config=/wlh/kafka03/config/kafka_server_jaas.conf"

开始执行启动kafka集群

# 生成一个uuid,后面需要用
/wlh/kafka01/bin/kafka-storage.sh random-uuid# 格式化存储
/wlh/kafka01/bin/kafka-storage.sh format -t xtzWWN4bTjitpL3kfd9s5g -c /wlh/kafka01/config/kraft/server.properties
/wlh/kafka02/bin/kafka-storage.sh format -t xtzWWN4bTjitpL3kfd9s5g -c /wlh/kafka02/config/kraft/server.properties
/wlh/kafka03/bin/kafka-storage.sh format -t xtzWWN4bTjitpL3kfd9s5g -c /wlh/kafka03/config/kraft/server.properties

开始启动kafka(-daemon后台启动)

# 分别启动它们
/wlh/kafka01/bin/kafka-server-start-saal.sh -daemon /wlh/kafka01/config/kraft/server.properties
/wlh/kafka02/bin/kafka-server-start-saal.sh -daemon /wlh/kafka02/config/kraft/server.properties
/wlh/kafka03/bin/kafka-server-start-saal.sh -daemon /wlh/kafka03/config/kraft/server.properties

在这里插入图片描述

服务启动完成。。。

Tip:服务器端口要打开,服务器端口要打开,端口打开!!!或者关了防火墙也行。

使用java进行连接

无论是使用kafka的API还是直接使用spring集成kafka都是可以的。

我这里就采用kafka的API方式了。

导入kafka-clients依赖

<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>2.7.2</version>
</dependency>

application.properties中配置相关属性,注意spring.kafka.jaas-config是结尾是有一个分号;的,若不写,是连接不到kafka的。

spring.kafka.bootstrap-servers=192.168.8.122:19092,192.168.8.122:29092,192.168.8.122:39092
spring.kafka.jaas-config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="kafka1234";
spring.kafka.topics=test

在java配置类中进行接收并且创建生产者和消费者

package xxx.xxx.xxx;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;import java.util.Properties;/*** @author wlh* @date 2023/8/10*/
@ConditionalOnProperty("spring.kafka.bootstrap-servers")
@Component
public class KafkaProperties {@Value("${spring.kafka.bootstrap-servers}")private String bootstrapServer;@Value("${spring.kafka.jaas-config}")private String jaasConfig;public static String topics;@Value("${spring.kafka.topics}")private void setTopics(String topics) {KafkaProperties.topics = topics;}/*** 获取生产者配置** @return 配置信息*/public Properties getProducerProperties() {Properties properties = new Properties();properties.put("bootstrap.servers", bootstrapServer);String SERIALIZER = "org.apache.kafka.common.serialization.StringSerializer";properties.put("key.serializer", SERIALIZER);properties.put("value.serializer", SERIALIZER);fillSecurityProperties(properties);return properties;}// 消费者配置public Properties getConsumerProperties() {Properties properties = new Properties();properties.put("bootstrap.servers", bootstrapServer);properties.put("group.id", "test");	// group.id可以自定义String DESERIALIZER = "org.apache.kafka.common.serialization.StringDeserializer";properties.put("key.deserializer", DESERIALIZER);properties.put("value.deserializer", DESERIALIZER);fillSecurityProperties(properties);return properties;}// 安全认证的配置private void fillSecurityProperties(Properties properties) {properties.setProperty("security.protocol", SecurityProtocol.SASL_PLAINTEXT.name);String SASL_MECHANISM = "PLAIN";properties.put(SaslConfigs.SASL_MECHANISM, SASL_MECHANISM);properties.put(SaslConfigs.SASL_JAAS_CONFIG, jaasConfig);}}

创建生产者和消费者

package xxx.xxx.xxx;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Arrays;
import java.util.Collections;
import java.util.List;/*** @author wlh* @date 2023/08/10*/
@ConditionalOnProperty("spring.kafka.bootstrap-servers")
@Slf4j
@RequiredArgsConstructor
@Configuration
public class KafkaConfig {private final KafkaProperties kafkaProperties;// 创建生产者@Beanpublic KafkaProducer<String, String> kafkaProducer() {return new KafkaProducer<>(kafkaProperties.getProducerProperties());}// 创建消费者@Beanpublic KafkaConsumer<String, String> kafkaConsumer() {KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(kafkaProperties.getConsumerProperties());List<String> topicList = Collections.singletonList("test"); // 这里写死了,可自行扩展kafkaConsumer.subscribe(topicList);log.info("消息订阅成功! topic:{}", topicList);log.info("消费者配置:{}", kafkaProperties.getConsumerProperties().toString());return kafkaConsumer;}}

信息发送的Util工具类

package xxx.xxx.xxx;import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.Collections;
import java.util.List;@Component
@Slf4j
public class KafkaSendUtil {@AutowiredKafkaProducer<String, String> kafkaProducer;@Asyncpublic void sendMsg(String topic, String msg) {List<String> topics;if (StringUtils.isBlank(topic)) {topics = Arrays.asList(KafkaProperties.topics.split(","));} else {topics = Collections.singletonList(topic);}for (String sendTopic : topics) {ProducerRecord<String, String> record = new ProducerRecord<>(sendTopic, msg);log.info("正在发送kafka数据,数据=====>{}", msg);kafkaProducer.send(record);}}}

实例

简单做一个实例,调通一下数据。监听方式可以不按照本文的,本文只是做测试。

kafka消费者监听器

package xxx.xxx.xxx;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;@Slf4j
@Component
public class KafkaListener implements ApplicationRunner {public static ExecutorService executorService = Executors.newFixedThreadPool(2);@Overridepublic void run(ApplicationArguments args) {log.info("监听服务启动!");executorService.execute(() -> {MessageHandler kafkaListenMessageHandler = SpringBeanUtils.getBean(MessageHandler.class);kafkaListenMessageHandler.onMessage(SpringBeanUtils.getBean("kafkaConsumer"), Arrays.asList("test"));	// 这里是监听的kafka的topic,这里写死了,自己扩展即可});}
}

Bean的工具类

package com.bjmetro.top.global.kafka;import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;@SuppressWarnings("unchecked")
@Component
public class SpringBeanUtils implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringBeanUtils.applicationContext = applicationContext;}public static <T> T getBean(String beanName) {if (applicationContext.containsBean(beanName)) {return (T) applicationContext.getBean(beanName);} else {return null;}}public static <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);}
}

消费者处理消息

package com.bjmetro.top.global.kafka;import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.springframework.stereotype.Component;import java.nio.charset.StandardCharsets;
import java.util.List;@Slf4j
@Component
public class MessageHandler {void onMessage(KafkaConsumer kafkaConsumer, List<String> topic) {log.info("队列开始监听:topic {}", topic);while (true) {ConsumerRecords<String, String> records = kafkaConsumer.poll(1000);for (ConsumerRecord<String, String> record : records) {log.info("partition:{} offset = {}, key = {}, value = {}", record.partition(), record.offset(), record.key(), record.value());try {String messageData = new String(record.value().getBytes(), StandardCharsets.UTF_8);System.out.println("收到消息:" + messageData);} catch (Exception e) {log.error("消息处理异常");}}}}}

做一个消息推送的接口

@Autowired
KafkaSendUtil sendUtil;
@PostMapping("/kafka/send")
public ResponseResult sendKafka(@RequestParam("msg") String msg) {sendUtil.sendMsg(null, msg);    // 这里topic传空,默认从application.properties中取了return new ResponseResult(ResponseConstant.CODE_OK, ResponseConstant.MSG_OK);
}

访问一下,看消费者日志

在这里插入图片描述


http://www.ppmy.cn/news/1074545.html

相关文章

【Docker】02-安装mysql

参考教程&#xff1a; https://www.bilibili.com/video/BV1Qa4y1t7YH/?p5&spm_id_frompageDriver&vd_source4964ba5015a16eb57d0ac13401b0fe77 docker安装Mysql 1、拉取最新版本的镜像 docker pull mysq:latestl 2、运行mysql服务 docker run --name mysql -e MYSQL_…

SpringBoot 使用 EMQX

一、SpringBoot服务器端 1. 在centos搭建 EMQX服务 2. 创建API密码 3. 在SpringBoot 的yml中添加mqqt的配置 #配置 emqx:ip: 47.109.49.176port: 18083api: xxxxxxxx &#xff08;自己的api&#xff09;secret: xxxxxxxxx &#xff08;自己的secret&#xff09; 4. 因为…

嵌入式Linux开发实操(十五):nand flash接口开发

# 前言 flash memory,分NAND和NOR: 如果说nor flash有个特点就是能执行代码,NOR并行接口具有地址和数据总线,spi flash更是主要用于存储代码,SPI(或QSPI)NOR代码可就地执行(XiP),一般系统要求flash闪存提供相对较高的频率和数据缓存的clocking。而nand flash主要用于…

【2023百度之星备赛】码蹄集 BD202301 公园(BFS求最短路)

题目 https://www.matiji.net/exam/brushquestion/1/4347/179CE77A7B772D15A8C00DD8198AAC74?from1 题目大意&#xff1a; 给定一个无向图&#xff0c;有两个人往同一个目的地走&#xff0c;分别消耗体力TE、FE。如果他们到某个点汇合了&#xff0c;然后一起走向目的地&…

Unity——协程(Coroutine)

本文为问GPT所得 一、在Unity中&#xff0c;协程到底是个啥 在Unity中&#xff0c;协程&#xff08;Coroutine&#xff09;是一种特殊的函数&#xff0c;用于在一段时间内暂停执行&#xff0c;并在稍后的时间点继续执行。通常情况下&#xff0c;我们在代码中通过调用协程来实现…

大集合按照指定长度进行分割成多个小集合,用于批量多次处理数据

&#x1f4da;目录 拆分案例拆分的核心代码 通常我们对集合的更新或者保存都需要用集合来承载通过插入的效率&#xff0c;但是这个会遇到一个问题就是你不知道那天那个集合的数量可能就超了&#xff0c;虽然我们连接数据库进行批量提交会在配置上配置allowMultiQueriestrue,但是…

爬虫逆向实战(二十八)--某税网第一步登录

一、数据接口分析 主页地址&#xff1a;某税网 1、抓包 通过抓包可以发现登录接口是factorAccountLogin 2、判断是否有加密参数 请求参数是否加密&#xff1f; 通过查看载荷模块可以发现有一个datagram 和 一个signature加密参数 请求头是否加密&#xff1f; 通过查看“标…

TCP连接分析:探寻TCP的三次握手

文章目录 一、实验背景与目的二、实验需求三、实验解法1. 预先抓包监测使用Wireshark工具2.进行TCP三次握手&#xff0c;访问www.baidu.com3.分析Wireshark捕获的TCP包 摘要&#xff1a; 本实验使用Wireshark工具&#xff0c;通过抓包监测和分析&#xff0c;深入研究了与百度服…