Skip to content

Commit

Permalink
feat(chat) : STOMP 유저 인증 프레임 인터셉터 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
waterfogSW committed Feb 2, 2025
1 parent c0df2aa commit 96ca871
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -24,4 +27,7 @@ class WebSocketConfig(
.withSockJS()
}

override fun configureClientInboundChannel(registration: ChannelRegistration) {
registration.interceptors(stompAuthInterceptor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 96ca871

Please sign in to comment.