This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
299a752
commit 79ac873
Showing
8 changed files
with
145 additions
and
12 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
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 |
---|---|---|
|
@@ -27,4 +27,8 @@ data class UserAuthentication( | |
} | ||
} | ||
|
||
override fun getName(): String { | ||
return userId.toString() | ||
} | ||
|
||
} |
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
...lin/com/studentcenter/weave/bootstrap/common/security/interceptor/StompAuthInterceptor.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.studentcenter.weave.bootstrap.common.security.interceptor | ||
|
||
import com.studentcenter.weave.application.user.service.util.UserTokenService | ||
import com.studentcenter.weave.application.user.vo.UserAuthentication | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.MessageChannel | ||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor | ||
import org.springframework.messaging.simp.stomp.StompCommand | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor | ||
import org.springframework.messaging.support.ChannelInterceptor | ||
import org.springframework.messaging.support.MessageHeaderAccessor | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class StompAuthInterceptor( | ||
private val userTokenService: UserTokenService, | ||
) : ChannelInterceptor { | ||
|
||
override fun preSend( | ||
message: Message<*>, | ||
channel: MessageChannel, | ||
): Message<*>? { | ||
val accessor = StompHeaderAccessor.wrap(message) | ||
val command = accessor.command | ||
|
||
when (command) { | ||
StompCommand.CONNECT -> handleConnect(message) | ||
else -> return message | ||
} | ||
|
||
return message | ||
} | ||
|
||
private fun handleConnect(message: Message<*>) { | ||
val accessor = MessageHeaderAccessor | ||
.getAccessor(message, SimpMessageHeaderAccessor::class.java) | ||
?: throw IllegalStateException("Cannot get accessor") | ||
|
||
extractToken(message)?.let { token -> | ||
val userAuthentication = userTokenService | ||
.resolveAccessToken(token) | ||
.let { UserAuthentication.from(it) } | ||
accessor.user = userAuthentication | ||
} | ||
} | ||
|
||
|
||
private fun extractToken(message: Message<*>): String? { | ||
val accessor = StompHeaderAccessor.wrap(message) | ||
val bearerToken: String? = accessor.getFirstNativeHeader(AUTHORIZATION_HEADER) | ||
return if (bearerToken != null && bearerToken.startsWith(BEARER_PREFIX)) { | ||
bearerToken.substring(BEARER_PREFIX.length) | ||
} else null | ||
} | ||
|
||
companion object { | ||
|
||
private const val AUTHORIZATION_HEADER = "Authorization" | ||
private const val BEARER_PREFIX = "Bearer " | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...in/com/studentcenter/weave/bootstrap/common/security/interceptor/StompExceptionHandler.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,55 @@ | ||
package com.studentcenter.weave.bootstrap.common.security.interceptor | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.studentcenter.weave.bootstrap.common.exception.ErrorResponse | ||
import com.studentcenter.weave.support.common.exception.CustomException | ||
import com.studentcenter.weave.support.common.exception.SystemExceptionType | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.simp.stomp.StompCommand | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor | ||
import org.springframework.messaging.support.MessageBuilder | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.socket.messaging.StompSubProtocolErrorHandler | ||
|
||
@Component | ||
class StompExceptionHandler( | ||
private val objectMapper: ObjectMapper, | ||
) : StompSubProtocolErrorHandler() { | ||
|
||
override fun handleClientMessageProcessingError( | ||
clientMessage: Message<ByteArray>?, | ||
ex: Throwable, | ||
): Message<ByteArray>? { | ||
return when (val cause: Throwable? = ex.cause) { | ||
is CustomException -> handleCustomException(cause) | ||
else -> handleException(ex) | ||
} | ||
} | ||
|
||
private fun handleCustomException(customException: CustomException): Message<ByteArray> { | ||
val response = ErrorResponse( | ||
customException.type.code, | ||
customException.message | ||
) | ||
val accessor = StompHeaderAccessor.create(StompCommand.ERROR) | ||
accessor.setLeaveMutable(true) | ||
return MessageBuilder.createMessage( | ||
objectMapper.writeValueAsBytes(response), | ||
accessor.messageHeaders | ||
) | ||
} | ||
|
||
private fun handleException(ex: Throwable): Message<ByteArray> { | ||
val response = ErrorResponse( | ||
SystemExceptionType.INTERNAL_SERVER_ERROR.code, | ||
ex.message.toString(), | ||
) | ||
val accessor = StompHeaderAccessor.create(StompCommand.ERROR) | ||
accessor.setLeaveMutable(true) | ||
return MessageBuilder.createMessage( | ||
objectMapper.writeValueAsBytes(response), | ||
accessor.messageHeaders | ||
) | ||
} | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
...rity/src/main/kotlin/com/studentcenter/weave/support/security/authority/Authentication.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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package com.studentcenter.weave.support.security.authority | ||
|
||
interface Authentication | ||
import java.security.Principal | ||
|
||
interface Authentication : Principal |