kafka+Kraft模式集群+安全认证_kafka raft 下的安全控制
/**
* 获取生产者配置
*
* @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;
// 创建生产者
@Bean
public KafkaProducer<String, String> kafkaProducer() {
return new KafkaProducer<>(kafkaProperties.getProducerProperties());
}
// 创建消费者
@Bean
public KafkaConsumer<String, String> kafkaConsumer() {
KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>
(kafkaProperties.getConsumerProperties());
List 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 {
@Autowired
KafkaProducer<String, String> kafkaProducer;
@Async
public void sendMsg(String topic, String msg) {
List 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);
@Override
public 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;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringBeanUtils.applicationContext = applicationContext;
}
public static T getBean(String beanName) {
if (applicationContext.containsBean(beanName)) {
return (T) applicationContext.getBean(beanName);
} else {
return null;
}
}
public static T getBean(Class clazz) {
return applicationContext.getBean(clazz);
}
}
消费者处理消息
package com.bjmetro.top.global.kafka;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数网络安全工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。






既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上网络安全知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注网络安全获取)

一、网安学习成长路线图
网安所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、网安视频合集
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

三、精品网安学习书籍
当我学到一定基础,有自己的理解能力的时候,会去阅读一些前辈整理的书籍或者手写的笔记资料,这些笔记详细记载了他们对一些技术点的理解,这些理解是比较独到,可以学到不一样的思路。

四、网络安全源码合集+工具包
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

五、网络安全面试题
最后就是大家最关心的网络安全面试题板块


一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

dnimg.cn/15c1192cad414044b4dd41f3df44433d.png)
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-gMTgcZpu-1712501826270)]
更多推荐
所有评论(0)