-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature :: 소켓 애러 핸들링 #333
- Loading branch information
Showing
9 changed files
with
176 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...main/kotlin/com/seugi/api/domain/chat/presentation/websocket/handler/StompErrorHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.seugi.api.domain.chat.presentation.websocket.handler | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.seugi.api.global.response.ErrorResponse | ||
import io.jsonwebtoken.ExpiredJwtException | ||
import io.jsonwebtoken.MalformedJwtException | ||
import io.jsonwebtoken.UnsupportedJwtException | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.MessageDeliveryException | ||
import org.springframework.messaging.simp.stomp.StompCommand | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor | ||
import org.springframework.messaging.support.MessageBuilder | ||
import org.springframework.security.access.AccessDeniedException | ||
import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler | ||
import java.nio.charset.StandardCharsets | ||
import java.security.SignatureException | ||
|
||
@Configuration | ||
class StompErrorHandler(private val objectMapper: ObjectMapper) : StompSubProtocolErrorHandler() { | ||
|
||
override fun handleClientMessageProcessingError(clientMessage: Message<ByteArray>?, ex: Throwable): Message<ByteArray>? { | ||
|
||
return when (ex) { | ||
is MessageDeliveryException -> { | ||
when (val cause = ex.cause) { | ||
is AccessDeniedException -> { | ||
sendErrorMessage(ErrorResponse(status = 4403, message = "Access denied")) | ||
} | ||
else -> { | ||
if (isJwtException(cause)) { | ||
sendErrorMessage(ErrorResponse(status = 4403, message = cause?.message ?: "JWT Exception")) | ||
} else { | ||
sendErrorMessage(ErrorResponse(status = 4403, message = cause?.stackTraceToString() ?: "Unhandled exception")) | ||
} | ||
} | ||
} | ||
} | ||
else -> { | ||
sendErrorMessage(ErrorResponse(status = 4400, message = ex.message ?: "Unhandled root exception")) | ||
} | ||
} | ||
} | ||
|
||
private fun isJwtException(ex: Throwable?): Boolean { | ||
return ex is SignatureException || ex is ExpiredJwtException || ex is MalformedJwtException || ex is UnsupportedJwtException || ex is IllegalArgumentException | ||
} | ||
|
||
private fun sendErrorMessage(errorResponse: ErrorResponse): Message<ByteArray> { | ||
val headers = StompHeaderAccessor.create(StompCommand.ERROR).apply { | ||
message = errorResponse.message | ||
} | ||
return try { | ||
val json = objectMapper.writeValueAsString(errorResponse) | ||
MessageBuilder.createMessage(json.toByteArray(StandardCharsets.UTF_8), headers.messageHeaders) | ||
} catch (e: JsonProcessingException) { | ||
MessageBuilder.createMessage(errorResponse.message.toByteArray(StandardCharsets.UTF_8), headers.messageHeaders) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/seugi/api/domain/chat/presentation/websocket/util/SecurityUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.seugi.api.domain.chat.presentation.websocket.util | ||
|
||
import com.seugi.api.global.auth.jwt.JwtUserDetails | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import java.security.Principal | ||
|
||
object SecurityUtils { | ||
|
||
fun getUserId(principal: Principal?): Long { | ||
return (principal as? UsernamePasswordAuthenticationToken)?.principal.let { it as? JwtUserDetails }?.member?.id?.value | ||
?: -1 | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tation/websocket/config/RabbitMQConfig.kt → ...seugi/api/global/config/RabbitMQConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/seugi/api/global/exception/CustomSocketExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.seugi.api.global.exception | ||
|
||
import com.seugi.api.global.response.ErrorResponse | ||
import org.springframework.messaging.handler.annotation.MessageExceptionHandler | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.simp.SimpMessagingTemplate | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import java.io.IOException | ||
import java.net.SocketException | ||
import java.security.Principal | ||
|
||
@ControllerAdvice | ||
class CustomSocketExceptionHandler( | ||
private val template: SimpMessagingTemplate | ||
) { | ||
|
||
private val bindingUrl = "/queue/errors" | ||
|
||
@MessageExceptionHandler(SocketException::class) | ||
fun handleSocketException(message: Message<*>, principal: Principal, ex: Exception) { | ||
removeSession(message) | ||
template.convertAndSendToUser( | ||
principal.name, | ||
bindingUrl, | ||
ErrorResponse(status = 4500, message = ex.cause?.message ?: "Socket Error") | ||
) | ||
} | ||
|
||
@MessageExceptionHandler(RuntimeException::class) | ||
fun handleRuntimeException(principal: Principal, ex: Exception) { | ||
template.convertAndSendToUser( | ||
principal.name, | ||
bindingUrl, | ||
ErrorResponse(status = 4500, message = ex.cause?.stackTraceToString() ?: "Socket Error") | ||
) | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun removeSession(message: Message<*>) { | ||
val stompHeaderAccessor = StompHeaderAccessor.wrap(message) | ||
val sessionId = stompHeaderAccessor.sessionId | ||
stompHeaderAccessor.sessionAttributes?.remove(sessionId) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/com/seugi/api/global/response/ErrorResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.seugi.api.global.response | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
data class ErrorResponse( | ||
override val status: Int = 4500, | ||
override val success: Boolean = false, | ||
override val state: String = "Error", | ||
override val message: String, | ||
) : ResponseInterface |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/seugi/api/global/response/ResponseInterface.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.seugi.api.global.response | ||
|
||
interface ResponseInterface { | ||
val status: Int | ||
val success: Boolean | ||
val state: String | ||
val message: String | ||
} |