kerberos整合kafka、springboot
文章目录一、背景:二、kerberos整合kafka三、kerberos整合java+kafka四、kerberos整合springboot+kafka一、背景:在192.168.248.100的主机上搭建kafka服务器,需要做kafka访问的一个安全验证!本文是建立在kerberos服务器已经搭建的情况下,搭建教程:https://blog.csdn.net/weixin_40496191/a
一、背景:
在192.168.248.100的主机上搭建kafka服务器,需要做kafka访问的一个安全验证!
本文是建立在kerberos服务器已经搭建的情况下,搭建教程:
https://blog.csdn.net/weixin_40496191/article/details/124056421
二、kerberos整合kafka
-
kerberos
服务器创建kafka
用户:addprinc -randkey kafka/192.168.248.100
-
生成凭据:
ktadd -k /root/kerberos/kafka.keytab -norandkey kafka/192.168.248.100
-
将凭据拷贝到kafka服务器的
/opt/kafka/kerberos
目录底下 -
在kafka安装目录下
config
创建kafka-jaas.conf
KafkaServer { com.sun.security.auth.module.Krb5LoginModule required useKeyTab=true storeKey=true keyTab="/opt/kafka/kerberos/kafka.keytab" principal="kafka/192.168.248.100@HADOOP.COM"; }; KafkaClient { com.sun.security.auth.module.Krb5LoginModule required useKeyTab=true useTicketCache=true keyTab="/opt/kafka/kerberos/kafka.keytab" principal="kafka/192.168.248.100@HADOOP.COM"; };
-
kafka修改安装目录下
config/server.properties
配置:advertised.listeners=SASL_PLAINTEXT://192.168.248.100:9092 listeners=SASL_PLAINTEXT://192.168.248.100:9092 security.inter.broker.protocol=SASL_PLAINTEXT sasl.mechanism.inter.broker.protocol=GSSAPI sasl.enabled.mechanisms=GSSAPI sasl.kerberos.service.name=kafka
-
将
kerberos
服务器的krb5.conf
移到/etc
目录底下 -
修改
bin/kafka-run-class.sh
脚本,添加kafka jvm
参数,,如下KAFKA_JVM_PERFORMANCE_OPTS="-server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+ExplicitGCInvokesConcurrent -XX:MaxInlineLevel=15 -Djava.awt.headless=true -Djava.awt.headless=true -Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=/opt/kafka/kafka1/config/kafka-jaas.conf"
-
修改
config/producer.properties
配置文件security.protocol = SASL_PLAINTEXT sasl.mechanism = GSSAPI sasl.kerberos.service.name =kafka
-
修改
config/consumer.properties
配置文件security.protocol = SASL_PLAINTEXT sasl.mechanism = GSSAPI sasl.kerberos.service.name=kafka
-
测试,启动kafka
生产:./kafka-console-producer.sh --broker-list 192.168.248.100:9092 --topic TOPIC1 --producer.config /opt/kafka/kafka1/config/producer.properties
消费:
./kafka-console-consumer.sh --bootstrap-server 192.168.248.100:9092 --topic TOPIC1--consumer.config /opt/kafka/kafka1/config/consumer.properties
三、kerberos整合java+kafka
-
kerberos整合java代码
package cn.Api; import kafka.utils.ZkUtils; import org.I0Itec.zkclient.ZkClient; import org.apache.kafka.clients.admin.AdminClient; import org.apache.kafka.clients.admin.DeleteTopicsResult; import org.apache.kafka.clients.admin.ListTopicsResult; import org.apache.kafka.common.KafkaFuture; import java.util.ArrayList; import java.util.Properties; import java.util.Set; import java.util.concurrent.ExecutionException; /** * @author 天真热 * @create 2022-04-06 15:09 * @desc **/ public class KerberosApi { public static void main(String[] args) { listTopic(); } /** * 查看主题列表 */ public static void listTopic() { Properties prop = new Properties(); System.setProperty("java.security.krb5.conf", "C:/Users/86188/Desktop/kerberos/krb5.conf"); //认证代码 System.setProperty("java.security.auth.login.config", "C:/Users/86188/Desktop/kerberos/kafka-jaas.conf");//认证代码 prop.put("sasl.kerberos.service.name", "kafka"); //认证代码 prop.put("sasl.mechanism", "GSSAPI"); //认证代码 prop.put("security.protocol", "SASL_PLAINTEXT"); //认证代码 prop.put("bootstrap.servers", "192.168.248.100:9092"); AdminClient admin = AdminClient.create(prop); ListTopicsResult result = admin.listTopics(); KafkaFuture<Set<String>> future = result.names(); try { System.out.println("==================Kafka Topics===================="); future.get().forEach(name -> System.out.println(name)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } /** * 删除主题 */ public static void deleteTopic() { //删除kafka主题 Properties prop = new Properties(); System.setProperty("java.security.krb5.conf", "C:/Users/86188/Desktop/kerberos/krb5.conf"); //认证代码 System.setProperty("java.security.auth.login.config", "C:/Users/86188/Desktop/kerberos/kafka-jaas.conf");//认证代码 prop.put("sasl.kerberos.service.name", "kafka"); //认证代码 prop.put("sasl.mechanism", "GSSAPI"); //认证代码 prop.put("security.protocol", "SASL_PLAINTEXT"); //认证代码 prop.put("bootstrap.servers", "192.168.248.100:9092"); AdminClient client = AdminClient.create(prop); ArrayList<String> topics = new ArrayList<>(); topics.add("test_topic"); DeleteTopicsResult result = client.deleteTopics(topics); try { result.all().get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }
四、kerberos整合springboot+kafka
- 将以下三个文件存在到项目所在服务器的文件夹中,并且修改
kafka-jaas.conf
配置文件的keytab
证书位置:kafka.keytab
、kafka-jaas.conf
、krb5.conf
- 项目配置启动参数:
-Djava.security.auth.login.config=C:/Users/86188/Desktop/kerberos/kafka-jaas.conf -Djava.security.krb5.conf=C:/Users/86188/Desktop/kerberos/krb5.conf
- 项目yml配置:
spring: kafka: properties: sasl: mechanism: GSSAPI kerberos: service: name: kafka jass: enabled: true security: protocol: SASL_PLAINTEXT
更多推荐
所有评论(0)