Add aop to record event log
This commit is contained in:
4
pom.xml
4
pom.xml
@@ -93,6 +93,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-security</artifactId>
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
39
src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt
Normal file
39
src/main/kotlin/top/fatweb/api/aop/EventLogAspect.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,8 +7,10 @@ import org.slf4j.LoggerFactory
|
|||||||
import org.springframework.security.authentication.AuthenticationManager
|
import org.springframework.security.authentication.AuthenticationManager
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import top.fatweb.api.annotation.EventLogRecord
|
||||||
import top.fatweb.api.entity.permission.LoginUser
|
import top.fatweb.api.entity.permission.LoginUser
|
||||||
import top.fatweb.api.entity.permission.User
|
import top.fatweb.api.entity.permission.User
|
||||||
|
import top.fatweb.api.entity.system.EventLog
|
||||||
import top.fatweb.api.exception.TokenHasExpiredException
|
import top.fatweb.api.exception.TokenHasExpiredException
|
||||||
import top.fatweb.api.properties.SecurityProperties
|
import top.fatweb.api.properties.SecurityProperties
|
||||||
import top.fatweb.api.service.permission.IAuthenticationService
|
import top.fatweb.api.service.permission.IAuthenticationService
|
||||||
@@ -39,6 +41,7 @@ class AuthenticationServiceImpl(
|
|||||||
) : IAuthenticationService {
|
) : IAuthenticationService {
|
||||||
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
|
||||||
|
|
||||||
|
@EventLogRecord(EventLog.Event.LOGIN)
|
||||||
override fun login(request: HttpServletRequest, user: User): LoginVo {
|
override fun login(request: HttpServletRequest, user: User): LoginVo {
|
||||||
val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(user.username, user.password)
|
val usernamePasswordAuthenticationToken = UsernamePasswordAuthenticationToken(user.username, user.password)
|
||||||
val authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken)
|
val authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken)
|
||||||
@@ -70,6 +73,7 @@ class AuthenticationServiceImpl(
|
|||||||
return LoginVo(jwt, loginUser.user.currentLoginTime, loginUser.user.currentLoginIp)
|
return LoginVo(jwt, loginUser.user.currentLoginTime, loginUser.user.currentLoginIp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EventLogRecord(EventLog.Event.LOGOUT)
|
||||||
override fun logout(token: String): Boolean {
|
override fun logout(token: String): Boolean {
|
||||||
val loginUser = WebUtil.getLoginUser() ?: let { throw TokenHasExpiredException() }
|
val loginUser = WebUtil.getLoginUser() ?: let { throw TokenHasExpiredException() }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user