Add aop to record event log

This commit is contained in:
2023-12-18 14:35:55 +08:00
parent 66146cb3b4
commit e3d31bcc38
4 changed files with 56 additions and 0 deletions

View File

@@ -93,6 +93,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

View File

@@ -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
)

View File

@@ -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)
}
}
}

View File

@@ -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() }