SUPPORT-8381: add kafka auth config

This commit is contained in:
Alexandr Shalaginov 2024-07-19 14:01:35 +03:00
parent 1f6e79466e
commit 519a6f4f74

View file

@ -3,7 +3,9 @@ package ervu;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
@ -19,6 +21,16 @@ import org.springframework.kafka.core.ProducerFactory;
public class KafkaProducerConfig {
@Value("${kafka.send.url:#{null}}")
private String kafkaUrl;
@Value("${kafka.send.security.protocol:#{null}}")
private String securityProtocol;
@Value("${kafka.send.login.module:org.apache.kafka.common.security.scram.ScramLoginModule}")
private String loginModule;
@Value("${kafka.send.username:#{null}}")
private String username;
@Value("${kafka.send.password:#{null}}")
private String password;
@Value("${kafka.sasl.mechanism:#{null}}")
private String saslMechanism;
@Bean
public ProducerFactory<String, String> producerFactory() {
@ -34,6 +46,14 @@ public class KafkaProducerConfig {
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, this.kafkaUrl);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
if (securityProtocol != null && username != null && password != null && saslMechanism != null) {
props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, securityProtocol);
props.put(SaslConfigs.SASL_JAAS_CONFIG, loginModule + " required username=\""
+ username + "\" password=\"" + password + "\";");
props.put(SaslConfigs.SASL_MECHANISM, saslMechanism);
}
return props;
}