kafka添加安全驗(yàn)證配置方式
- 綜合考慮性能影響、管理成本、安全等級(jí)要求,接入便利程度。
- 鑒權(quán)采用SASL+PLAINTEXT 方式。
- 每個(gè)集群會(huì)分配統(tǒng)一的訪問(wèn)賬號(hào)及密碼用于客戶端訪問(wèn)。
服務(wù)端配置
1. config 目錄添加kafka_server_jaas.conf 配置文件
內(nèi)容:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="7f8d9dsf789ds7ffsdfdsfu9"
user_admin="7f8d9dsf789ds7ffsdfdsfu9"
user_alice="xjfkddjfdssifds";
};2. kafka-run-class.sh 添加
KAFKA_OPTS="$KAFKA_OPTS -Djava.security.auth.login.config=/home/finance/App/kafka_2.12-2.5.1/config/kafka_server_jaas.conf"
3. config/server.properties
添加
security.inter.broker.protocol=SASL_PLAINTEXT sasl.mechanism.inter.broker.protocol=PLAIN sasl.enabled.mechanisms=PLAIN listener, advertised.listeners 添加對(duì)應(yīng)SASL_PLAINTEXT 監(jiān)聽器 listeners=SASL_PLAINTEXT://10.193.196.112:9092 advertised.listeners=SASL_PLAINTEXT://10.193.196.112:9092
客戶端接入改造
就是在java程序中將安全參數(shù)配置進(jìn)來(lái)
生產(chǎn)者、消費(fèi)者屬性配置加入一下配置
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=alice password=alice;");
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");springboot 高版本估計(jì)有支持在properties文件中直接配置,此處沒(méi)有驗(yàn)證。
完整配置示例
此方法,可以用KafkaProperties 獲取properties配置文件中的參數(shù)后,再往里面添加新的參數(shù)
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import java.util.Map;
/**
* kafka配置
*/
@Configuration
@EnableKafka
public class KafkaProducerConfig {
@Autowired
private KafkaProperties kafkaProperties;
/**
* 消費(fèi)者配置
*/
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
Map<String, Object> props = kafkaProperties.buildConsumerProperties();
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=user1 password=pass1;");
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");
factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(props));
factory.setConcurrency(2);
factory.getContainerProperties().setPollTimeout(1500);
return factory;
}
/**
* 生產(chǎn)者配置
*/
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
Map<String, Object> props = kafkaProperties.buildProducerProperties();
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=user1 password=pass1;");
props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");
return new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(props));
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java的MyBatis框架中Mapper映射配置的使用及原理解析
Mapper用于映射SQL語(yǔ)句,可以說(shuō)是MyBatis操作數(shù)據(jù)庫(kù)的核心特性之一,這里我們來(lái)討論Java的MyBatis框架中Mapper映射配置的使用及原理解析,包括對(duì)mapper的xml配置文件的讀取流程解讀.2016-06-06
Kafka使用Java客戶端進(jìn)行訪問(wèn)的示例代碼
本篇文章主要介紹了Kafka使用Java客戶端進(jìn)行訪問(wèn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能
這篇文章主要介紹了多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java8 HashMap的實(shí)現(xiàn)原理分析
Java8之后新增挺多新東西,接下來(lái)通過(guò)本文給大家介紹Java8 HashMap的實(shí)現(xiàn)原理分析,對(duì)java8 hashmap實(shí)現(xiàn)原理相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03

