From 96ca8718e02809bf729b2fecf976447f45d75982 Mon Sep 17 00:00:00 2001 From: San Kim Date: Sun, 2 Feb 2025 18:50:18 +0900 Subject: [PATCH] =?UTF-8?q?feat(chat)=20:=20STOMP=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=9D=B8=EC=A6=9D=20=ED=94=84=EB=A0=88=EC=9E=84=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=EC=85=89=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bootstrap/api/support/config/WebSocketConfig.kt | 6 ++++++ .../support/security/interceptor/StompAuthInterceptor.kt | 8 +++++++- .../com/threedays/redis/support/config/RedisConfig.kt | 6 ++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/config/WebSocketConfig.kt b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/config/WebSocketConfig.kt index 48ccec8..010b3e3 100644 --- a/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/config/WebSocketConfig.kt +++ b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/config/WebSocketConfig.kt @@ -1,6 +1,8 @@ package com.threedays.bootstrap.api.support.config +import com.threedays.bootstrap.api.support.security.interceptor.StompAuthInterceptor import org.springframework.context.annotation.Configuration +import org.springframework.messaging.simp.config.ChannelRegistration import org.springframework.messaging.simp.config.MessageBrokerRegistry import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker import org.springframework.web.socket.config.annotation.StompEndpointRegistry @@ -10,6 +12,7 @@ import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerCo @EnableWebSocketMessageBroker class WebSocketConfig( private val properties: WebSocketProperties, + private val stompAuthInterceptor: StompAuthInterceptor, ) : WebSocketMessageBrokerConfigurer { override fun configureMessageBroker(registry: MessageBrokerRegistry) { @@ -24,4 +27,7 @@ class WebSocketConfig( .withSockJS() } + override fun configureClientInboundChannel(registration: ChannelRegistration) { + registration.interceptors(stompAuthInterceptor) + } } diff --git a/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/security/interceptor/StompAuthInterceptor.kt b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/security/interceptor/StompAuthInterceptor.kt index 41f206c..99a13eb 100644 --- a/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/security/interceptor/StompAuthInterceptor.kt +++ b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/security/interceptor/StompAuthInterceptor.kt @@ -4,6 +4,7 @@ import com.threedays.application.auth.config.AuthProperties import com.threedays.domain.auth.entity.AccessToken import com.threedays.domain.chat.entity.Session import com.threedays.domain.chat.repository.SessionRepository +import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.messaging.Message import org.springframework.messaging.MessageChannel import org.springframework.messaging.simp.stomp.StompCommand @@ -21,6 +22,7 @@ class StompAuthInterceptor( private const val AUTHORIZATION_HEADER = "Authorization" private const val BEARER_PREFIX = "Bearer " + private val logger = KotlinLogging.logger {} } override fun preSend( @@ -52,12 +54,16 @@ class StompAuthInterceptor( secret = authProperties.tokenSecret, ) .let { Session.create(sessionId, it.userId) } - .also { sessionRepository.save(it) } + .also { + sessionRepository.save(it) + logger.info { "User ${it.userId} connected with session $sessionId" } + } } } private fun handleDisconnect(sessionId: Session.Id) { sessionRepository.deleteById(sessionId) + logger.info { "Session $sessionId disconnected" } } private fun extractToken(message: Message<*>): String? { diff --git a/infrastructure/redis/src/main/kotlin/com/threedays/redis/support/config/RedisConfig.kt b/infrastructure/redis/src/main/kotlin/com/threedays/redis/support/config/RedisConfig.kt index 40ca712..c8d6df3 100644 --- a/infrastructure/redis/src/main/kotlin/com/threedays/redis/support/config/RedisConfig.kt +++ b/infrastructure/redis/src/main/kotlin/com/threedays/redis/support/config/RedisConfig.kt @@ -28,7 +28,6 @@ const val BASE_PACKAGE = "com.threedays.redis" @EnableRedisRepositories(basePackages = [BASE_PACKAGE]) class RedisConfig( private val redisProperties: RedisProperties, - private val messageSubscriberRedisAdapter: MessageSubscriberRedisAdapter ) { @Bean @@ -48,7 +47,10 @@ class RedisConfig( } @Bean - fun redisMessageListenerContainer(redisConnectionFactory: RedisConnectionFactory): RedisMessageListenerContainer { + fun redisMessageListenerContainer( + redisConnectionFactory: RedisConnectionFactory, + messageSubscriberRedisAdapter: MessageSubscriberRedisAdapter + ): RedisMessageListenerContainer { val container = RedisMessageListenerContainer() container.setConnectionFactory(redisConnectionFactory)