-
SpringBoot와 Kafka 연동 간의 SASL을 사용한 암호화 적용하기SpringBoot 2024. 1. 19. 00:44
최근에 카프카에 SASL 인증 방식을 적용한 상태여서
기존 스프링부트와 카프카 간의 연동에 더해서 스프링부트에 SASL 인증과 관련된 추가적인 설정이 필요하다.
이번에는 해당 설정들에 대해서 기록을 해보고자 한다.
https://kjungw1025.tistory.com/27
참고로 스프링부트와 카프카 연동하는 방법으로는 application.yml에 간단하게 관련 설정을 하는 방법과
직접 ProducerFactory, ConsumerFactory Bean을 등록하는 방법으로 총 2가지가 존재한다.
나의 경우 직접 Bean을 등록하는 방식으로 수행하였다.
이때 application.yml에 관련 설정을 하고, Bean에도 반영하면 application.yml에 설정한 내용들이 무시되므로
둘 중에 하나만 사용하는걸 추천한다.
📖 application.yml에 설정하는 방식
spring: kafka: bootstrap-servers: kafka-server:kafka-server-port ... properties: security.protocol: SASL_PLAINTEXT sasl.mechanism: PLAIN sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username="your-username" password="your-password"; ...
application.yml에 설정하는 방식으로는 위와 같이 properties 부분에 3줄을 추가해주면 된다.
SASL을 적용하신 분들은 각 3줄이 무엇을 의미하는지 알 것이다.
그래서 간단하게 정리하고 Bean 등록 방식으로 넘어가고자 한다.
security.protocol
Kafka 클라이언트가 브로커와 통신할 때 사용하는 보안 프로토콜을 지정하는 것으로
보통 PLAINTEXT, SSL, SASL_PLAINTEXT, SASL_SSL 중 하나를 선택한다.
여기서 SASL_PLAINTEXT는 간단한 인증 및 보안 레이어(SASL)를 사용한 텍스트 통신을 의미한다.
sasl.mechanism
SASL 인증 메커니즘을 지정하는 것으로, 주로 PLAIN과 GSSAPI 등이 사용된다.
PLAIN은 사용자 이름과 비밀번호를 평문으로 전송하는 간단한 인증 방법이다.
sasl.jaas.config
SASL 인증을 위한 JAAS (Java Authentication and Authorization Service) 설정을 지정한다.
주로 사용자 이름과 비밀번호를 설정하는 데 사용되어 PLAIN 메커니즘을 사용하는 경우,
org.apache.kafka.common.security.plain.PlainLoginModule 클래스를 사용하여 사용자 이름과 비밀번호를 설정한다.
📖 @Bean 등록 방식
우선 나의 경우 username과 password만 application.yml에 아래와 같이 넣은 후,
@Value로 해당 값들을 가져오는 방식으로 작성했다.
kafka: jaas: username: {jaas.conf에 등록한 username} password: {jaas.conf에 등록한 username에 대한 password}
KafkaProducerConfig.java
@EnableKafka @Configuration @RequiredArgsConstructor public class KafkaProducerConfig { ... @Value("${spring.kafka.jaas.username}") private String username; @Value("${spring.kafka.jaas.password}") private String password; ... @Bean public Map<String, Object> producerConfig() { ... props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT"); props.put(SaslConfigs.SASL_MECHANISM, "PLAIN"); props.put(SaslConfigs.SASL_JAAS_CONFIG, String.format( "%s required username=\"%s\" " + "password=\"%s\";", PlainLoginModule.class.getName(), username, password )); return props; } }
KafkaConsumerConfig.java
@EnableKafka @Configuration @RequiredArgsConstructor @Slf4j public class KafkaConsumerConfig { ... @Value("${spring.kafka.jaas.username}") private String username; @Value("${spring.kafka.jaas.password}") private String password; ... @Bean public ConsumerFactory<String, String> consumerFactory() { ... consumerConfig.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT"); consumerConfig.put(SaslConfigs.SASL_MECHANISM, "PLAIN"); consumerConfig.put(SaslConfigs.SASL_JAAS_CONFIG, String.format( "%s required username=\"%s\" " + "password=\"%s\";", PlainLoginModule.class.getName(), username, password )); return new DefaultKafkaConsumerFactory<>(consumerConfig, new StringDeserializer(), new StringDeserializer()); } ... }
'SpringBoot' 카테고리의 다른 글
Mockito 에러 - UnnecessaryStubbingException 해결 (0) 2024.03.21 쿠폰 발급 요청 시, 확인하는 쿠폰 정보를 Redis Cache에 담아 개선하기 (0) 2024.03.19 [Spring Boot] 채팅방에서 사용자가 업로드한 파일을 NHN Cloud의 Object Storage를 통해 관리하기 (0) 2024.02.07 Spring-Data-DynamoDB를 사용하여 SpringBoot와 AWS DynamoDB 연동하기 (0) 2024.01.28 [Spring Boot] 엑셀(xlsx) 파일 파싱 후, 서비스 로직에 추가하는 코드 작성하기 (0) 2023.12.21