Skip to content

Commit

Permalink
imp: sys_log에 uid 항목 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 2, 2024
1 parent 9a1bda5 commit ad8b0d9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions sql/DDL.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
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
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,
Expand Down
18 changes: 12 additions & 6 deletions src/main/kotlin/com/hero/alignlab/event/model/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
}
Expand Down

0 comments on commit ad8b0d9

Please sign in to comment.