diff --git a/sql/DDL.sql b/sql/DDL.sql index 296e3f9..d4a1349 100644 --- a/sql/DDL.sql +++ b/sql/DDL.sql @@ -6,6 +6,7 @@ DATABASE hero CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; CREATE TABLE `system_action_log` ( `id` bigint NOT NULL AUTO_INCREMENT, + `uid` int DEFAULT NULL COMMENT 'uid', `host` varchar(256) DEFAULT NULL, `http_method` varchar(256) DEFAULT NULL, `ip_address` varchar(256) DEFAULT NULL, diff --git a/src/main/kotlin/com/hero/alignlab/domain/log/domain/SystemActionLog.kt b/src/main/kotlin/com/hero/alignlab/domain/log/domain/SystemActionLog.kt index 5a0370b..5443676 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/log/domain/SystemActionLog.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/log/domain/SystemActionLog.kt @@ -10,6 +10,9 @@ class SystemActionLog( @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = -1L, + @Column(name = "uid") + val uid: Long? = null, + @Column(name = "ip_address") val ipAddress: String? = null, diff --git a/src/main/kotlin/com/hero/alignlab/event/listener/SystemActionLogEventListener.kt b/src/main/kotlin/com/hero/alignlab/event/listener/SystemActionLogEventListener.kt index 7cc9780..5b90c37 100644 --- a/src/main/kotlin/com/hero/alignlab/event/listener/SystemActionLogEventListener.kt +++ b/src/main/kotlin/com/hero/alignlab/event/listener/SystemActionLogEventListener.kt @@ -1,5 +1,8 @@ package com.hero.alignlab.event.listener +import com.hero.alignlab.domain.auth.application.AuthFacade +import com.hero.alignlab.domain.auth.model.AuthUser +import com.hero.alignlab.domain.auth.model.AuthUserToken import com.hero.alignlab.domain.log.application.SystemActionLogService import com.hero.alignlab.domain.log.domain.SystemActionLog import com.hero.alignlab.event.model.SystemActionLogEvent @@ -7,18 +10,30 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch +import kotlinx.coroutines.reactor.awaitSingleOrNull import org.springframework.context.event.EventListener import org.springframework.stereotype.Component +import reactor.kotlin.core.publisher.toMono @Component class SystemActionLogEventListener( private val systemActionLogService: SystemActionLogService, + private val authFacade: AuthFacade, ) { @EventListener fun subscribe(event: SystemActionLogEvent) { CoroutineScope(Dispatchers.IO + Job()).launch { + val authUser = event.token + ?.let { token -> AuthUserToken.from(token).toMono() } + ?.let { token -> + runCatching { + authFacade.resolveAuthUser(token).awaitSingleOrNull() as? AuthUser + }.getOrNull() + } + if (filterLog(event)) { SystemActionLog( + uid = authUser?.uid, ipAddress = event.ipAddress, path = event.path, httpMethod = event.method, diff --git a/src/main/kotlin/com/hero/alignlab/event/model/Event.kt b/src/main/kotlin/com/hero/alignlab/event/model/Event.kt index fafc6be..11e1e47 100644 --- a/src/main/kotlin/com/hero/alignlab/event/model/Event.kt +++ b/src/main/kotlin/com/hero/alignlab/event/model/Event.kt @@ -2,6 +2,7 @@ package com.hero.alignlab.event.model import com.hero.alignlab.common.extension.mapper import com.hero.alignlab.common.extension.remoteIp +import com.hero.alignlab.domain.auth.model.AUTH_TOKEN_KEY import com.hero.alignlab.domain.group.domain.Group import com.hero.alignlab.domain.pose.domain.PoseSnapshot import com.hero.alignlab.domain.pose.model.PoseSnapshotModel.KeyPoint @@ -30,23 +31,28 @@ data class SystemActionLogEvent( val userAgent: String?, val host: String?, val referer: String?, + val token: String?, val extra: String?, ) : BaseEvent() { companion object { - private const val USER_AGENT = "User-Agent" - private const val HOST = "Host" - private const val REFERER = "Referer" + private const val USER_AGENT = "USER-AGENT" + private const val HOST = "HOST" + private const val REFERER = "REFERER" fun from(exchange: ServerWebExchange): SystemActionLogEvent { val request = exchange.request + val headers = exchange.request.headers.toSingleValueMap() + .mapKeys { header -> header.key.uppercase() } + return SystemActionLogEvent( ipAddress = request.remoteIp, method = request.method.name(), path = request.uri.path, - userAgent = request.headers[USER_AGENT].toString(), - host = request.headers[HOST].toString(), - referer = request.headers[REFERER].toString(), + userAgent = headers[USER_AGENT], + host = headers[HOST], + referer = headers[REFERER], + token = headers[AUTH_TOKEN_KEY], extra = mapper.writeValueAsString(request.headers) ) }