2024年网络安全最全kafka+Kraft模式集群+安全认证_kafka raft 下的安全控制,网络安全面试问项目难点

写在最后

在结束之际,我想重申的是,学习并非如攀登险峻高峰,而是如滴水穿石般的持久累积。尤其当我们步入工作岗位之后,持之以恒的学习变得愈发不易,如同在茫茫大海中独自划舟,稍有松懈便可能被巨浪吞噬。然而,对于我们程序员而言,学习是生存之本,是我们在激烈市场竞争中立于不败之地的关键。一旦停止学习,我们便如同逆水行舟,不进则退,终将被时代的洪流所淘汰。因此,不断汲取新知识,不仅是对自己的提升,更是对自己的一份珍贵投资。让我们不断磨砺自己,与时代共同进步,书写属于我们的辉煌篇章。

需要完整版PDF学习资源私我

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以点击这里获取

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

<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="kafka" 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;

    // 创建生产者
    @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<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 {

    @Autowired
    KafkaProducer<String, String> kafkaProducer;

    @Async
    public 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);
    @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> 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);
}

访问一下,看消费者日志

在这里插入图片描述

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新网络安全全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以点击这里获取

[外链图片转存中…(img-wZoSgdVl-1715429217783)]
[外链图片转存中…(img-luarumCT-1715429217784)]
[外链图片转存中…(img-P4xtr3ZT-1715429217785)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上网络安全知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以点击这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值