【问题标题】:How can I set user and password for Kafka server?如何设置 Kafka 服务器的用户名和密码?
【发布时间】:2021-11-29 22:36:49
【问题描述】:

我是 Kafka 软件的初学者。我想在我的服务器上运行 Kafka。我运行它,但我无法设置密码,所有人都可以连接到我的服务器。如何设置没有人可以连接的密码Kafka?我用的是 Spring Boot。


我当前的配置...

@EnableKafka
@Configuration
@SuppressWarnings("SpringFacetCodeInspection")
public class KafkaConfig {

    @Bean
    ConcurrentKafkaListenerContainerFactory<String, String>
    kafkaListenerContainerFactory(ConsumerFactory<String, String> consumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory);
        return factory;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {

        final Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString());
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");

        return new DefaultKafkaConsumerFactory<>(props);

    }

    @Bean
    public ProducerFactory<String, String> producerFactory() {

        final Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.LINGER_MS_CONFIG, 10);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

        return new DefaultKafkaProducerFactory<>(props);

    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate(ProducerFactory<String, String> producerFactory) {
        return new KafkaTemplate<>(producerFactory);
    }

}

【问题讨论】:

  • 你的 kafka 服务器版本是多少?

标签: java spring kotlin apache-kafka


【解决方案1】:

在您的 application.yml 中:

spring:
  kafka:
    bootstrap-servers: <ip>:<port>
    properties:
      security:
        protocol: SASL_PLAINTEXT
      sasl:
        mechanism: PLAIN
        jaas:
          config: org.apache.kafka.common.security.plain.PlainLoginModule required username="my_user" password="my_password";

在配置类中:

props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
props.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
props.put("sasl.jaas.config", PlainLoginModule.class.getName() + " required username=\"" + my_user + "\" password=\"" + my_password + "\";")

更新

在你的server.properties:

listeners=PLAINTEXT://localhost:9092,SASL_PLAINTEXT://localhost:9093,SASL_SSL://localhost:9094
advertised.listeners=PLAINTEXT://localhost:9092,SASL_PLAINTEXT://localhost:9093,SASL_SSL://localhost:9094
security.inter.broker.protocol=SASL_SSL
ssl.endpoint.identification.algorithm=
ssl.client.auth=required
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-512
sasl.enabled.mechanisms=SCRAM-SHA-512
# Broker security settings
ssl.truststore.location=
<kafka-binary-dir>/config/truststore/kafka.truststore.jks
ssl.truststore.password=password
ssl.keystore.location=
<kafka-binary-dir>/config/keystore/kafka.keystore.jks
ssl.keystore.password=password
ssl.key.password=password
# ACLs
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
super.users=User:admin

【讨论】:

  • 我将我当前的配置添加到主帖。如何通过代码配置解决这个问题?
  • @MyName 我编辑了答案,用它为消费者设置密码。希望对您有所帮助
  • 好的。谢谢。但是如何在 KAFKA 中设置密码?我通过 kafka-server-start /usr/local/etc/kafka/server.properties 运行它,我需要在哪里设置密码?
  • @MyName 看看更新
  • 不工作 - localhost/127.0.0.1(与客户端机制 PLAIN 的意外握手请求,启用的机制是 [])
猜你喜欢
  • 2021-05-18
  • 2015-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 2017-08-17
相关资源
最近更新 更多