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)