From e3d31bcc3879321ba4d8f713aa6e37f61fa2fa03 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 18 Dec 2023 14:35:55 +0800 Subject: [PATCH] Add aop to record event log --- pom.xml | 4 ++ .../fatweb/api/annotation/EventLogRecord.kt | 9 +++++ .../top/fatweb/api/aop/EventLogAspect.kt | 39 +++++++++++++++++++ .../impl/AuthenticationServiceImpl.kt | 4 ++ 4 files changed, 56 insertions(+) create mode 100644 src/main/kotlin/top/fatweb/api/annotation/EventLogRecord.kt create mode 100644 src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt diff --git a/pom.xml b/pom.xml index 4ab8946..225ece4 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,10 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-aop + org.springframework.boot spring-boot-starter-security diff --git a/src/main/kotlin/top/fatweb/api/annotation/EventLogRecord.kt b/src/main/kotlin/top/fatweb/api/annotation/EventLogRecord.kt new file mode 100644 index 0000000..2cdda01 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/annotation/EventLogRecord.kt @@ -0,0 +1,9 @@ +package top.fatweb.api.annotation + +import top.fatweb.api.entity.system.EventLog + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.RUNTIME) +annotation class EventLogRecord( + val event: EventLog.Event +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt b/src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt new file mode 100644 index 0000000..87fb550 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt @@ -0,0 +1,39 @@ +package top.fatweb.api.aop + +import org.aspectj.lang.JoinPoint +import org.aspectj.lang.annotation.AfterReturning +import org.aspectj.lang.annotation.Aspect +import org.aspectj.lang.annotation.Pointcut +import org.aspectj.lang.reflect.MethodSignature +import org.slf4j.Logger +import org.slf4j.LoggerFactory +import org.springframework.stereotype.Component +import top.fatweb.api.annotation.EventLogRecord +import top.fatweb.api.entity.system.EventLog +import top.fatweb.api.service.system.IEventLogService +import top.fatweb.api.util.WebUtil + +@Aspect +@Component +class EventLogAspect( + private val eventLogService: IEventLogService +) { + private val logger: Logger = LoggerFactory.getLogger(this::class.java) + + @Pointcut("@annotation(top.fatweb.api.annotation.EventLogRecord)") + fun eventLogPointcut() {} + + @AfterReturning("eventLogPointcut()") + fun doAfter(joinPoint: JoinPoint) { + val annotation = (joinPoint.signature as MethodSignature).method.getAnnotation(EventLogRecord::class.java) + + try { + eventLogService.save(EventLog().apply { + this.event = annotation.event + operateUserId = WebUtil.getLoginUserId() ?: -1 + }) + } catch (e: Exception) { + logger.error("Cannot record event!!!", e) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt index e98291a..a9da6e0 100644 --- a/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/permission/impl/AuthenticationServiceImpl.kt @@ -7,8 +7,10 @@ import org.slf4j.LoggerFactory import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.stereotype.Service +import top.fatweb.api.annotation.EventLogRecord import top.fatweb.api.entity.permission.LoginUser import top.fatweb.api.entity.permission.User +import top.fatweb.api.entity.system.EventLog import top.fatweb.api.exception.TokenHasExpiredException import top.fatweb.api.properties.SecurityProperties import top.fatweb.api.service.permission.IAuthenticationService @@ -39,6 +41,7 @@ class AuthenticationServiceImpl( ) : IAuthenticationService { private val logger: Logger = LoggerFactory.getLogger(this::class.java) + @EventLogRecord(EventLog.Event.LOGIN) override fun login(request: HttpServletRequest, user: User): LoginVo { val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(user.username, user.password) val authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken) @@ -70,6 +73,7 @@ class AuthenticationServiceImpl( return LoginVo(jwt, loginUser.user.currentLoginTime, loginUser.user.currentLoginIp) } + @EventLogRecord(EventLog.Event.LOGOUT) override fun logout(token: String): Boolean { val loginUser = WebUtil.getLoginUser() ?: let { throw TokenHasExpiredException() }