0

I am confused how to add authentication credentials progrmatically. Not sure if SECURITY_PROVIDERS_CONFIG is where I set these values. Was also going through the constants and I could see many configs have _DOC and I am curious what does it mean. For example SECURITY_PROVIDERS_CONFIG and SECURITY_PROVIDERS_DOC.

Here is my config class:

@Configuration
public class KafkaProducerConfig {
    @Value(value = "${spring.kafka.bootstrap-servers}")
    private String bootstrapAddress;

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        Map<String, Object> configProps = new HashMap<>();
        configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
        configProps.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, CountingProducerInterceptor.class.getName());
        configProps.put(ProducerConfig.ACKS_CONFIG, "all");
        configProps.put(ProducerConfig.ENABLE_IDEMPOTENCE_DOC, "true");
        configProps.put(ProducerConfig.RETRIES_CONFIG, 3);
        //configProps.put(ProducerConfig.SECURITY_PROVIDERS_CONFIG, "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"username\" password=\"pa$$wrd\"");
        configProps.put(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 5);
        configProps.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, 10000);
        configProps.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 3000);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
        return new DefaultKafkaProducerFactory<>(configProps);
    }

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

I have commented out the property that I am confused. Not sure if this ishow you pass username and password. Thanks in advance.

Thomson Mathew
  • 419
  • 1
  • 9
  • 28

1 Answers1

0

It is not necessary to have an authentication for you Kafka server. Consider that you can containerize your Kafka instance in your server and you are not forced to expose your ports out of your physical server and your applications will communicate internally (e.g: using your docker-network inside the server).

But if you wish to implement an authentication for your Kafka server, It is also possible to have both SSL or Plain authentication!!

Here I found an useful answer that may be useful for you too:

kafka Authentication

Sobhan
  • 1,280
  • 1
  • 18
  • 31
  • thanks for the reply. Unfortunately our kafka server is in the cloud and producer an on-prem app. We do have both SSL and plain text authentication in prod but lower env its just plain text authentication. Its a legacy app and unfortunately I cant have any property files. Just looking for the config for username and password programatically. – Thomson Mathew Jan 04 '23 at 15:14