0

I can't find how to pass user name and password to KafkaConsumer. I use SASL PLAINTEXT to authenticate client on Kafka server.

I tried to find this info on https://kafka.apache.org/documentation/#consumerconfigs but a result was negative.

I found some example where an author used params "username" and "password". I tried to do this:

props.put("bootstrap.servers", uri.getHost() + ":" + uri.getPort());
if (!groupidName.isBlank())
    props.put("group.id", groupidName);
props.put("enable.auto.commit", "false");
props.put("auto.offset.reset", groupidName.isBlank() ? "latest" : "earliest");
props.put("session.timeout.ms", "30000");
props.put("max.poll.records", "1");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
if (kafkaConnector.getConfig().getUserName() != null && kafkaConnector.getConfig().getPassword() != null)
{
    props.put("security.protocol","SASL_PLAINTEXT");
    props.put("sasl.mechanism","PLAIN");
    props.put("username",kafkaConnector.getConfig().getUserName());
    props.put("password",kafkaConnector.getConfig().getPassword());
}

but i got the exception "Failed to construct kafka consumer".

If someone knows how to pass user name and password to consumer configuration, please help.

1 Answers1

2

You have to set the value of property sasl.jaas.config.

Here's what we do:

props.put("security.protocol", "SASL_PLAINTEXT");
props.put("sasl.mechanism", "PLAIN");
props.put("sasl.jaas.config", PlainLoginModule.class.getName() + " required username=\"" + KAFKA_USER + "\" password=\"" + KAFKA_PASSWORD + "\";")
lreeder
  • 12,047
  • 2
  • 56
  • 65