fix: AOP日志记录bug

This commit is contained in:
Bunny 2024-12-29 15:02:16 +08:00
parent 615f6323c5
commit 6cb6961b38
4 changed files with 22 additions and 7 deletions

View File

@ -17,6 +17,8 @@ import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -30,6 +32,18 @@ public class UserRequestLogAop {
@Autowired @Autowired
private UserRequestLogRepository userRequestLogRepository; private UserRequestLogRepository userRequestLogRepository;
private boolean isSerializable(Object obj) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(obj);
out.close();
return true;
} catch (Exception e) {
return false;
}
}
@Around(value = "execution(* cn.bunny.services.controller.*.*.*(..)) && !@annotation(cn.bunny.services.aop.annotation.ExcludeRequestLog)") @Around(value = "execution(* cn.bunny.services.controller.*.*.*(..)) && !@annotation(cn.bunny.services.aop.annotation.ExcludeRequestLog)")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable { public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
LoginVo loginVo = BaseContext.getLoginVo(); LoginVo loginVo = BaseContext.getLoginVo();
@ -52,8 +66,9 @@ public class UserRequestLogAop {
userRequestLog.setIpRegion(ipRegion); userRequestLog.setIpRegion(ipRegion);
userRequestLog.setExecuteTime(executeTime); userRequestLog.setExecuteTime(executeTime);
// 判断传入参数能否被序列化
userRequestLog.setArg(JSON.toJSONString(args)); Object saveArgs = isSerializable(args) ? JSON.toJSONString(args) : "";
userRequestLog.setArg(JSON.toJSONString(saveArgs));
// 当前请求request // 当前请求request
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

View File

@ -41,6 +41,7 @@ import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -60,6 +61,7 @@ import java.util.stream.Collectors;
* @since 2024-11-07 16:49:19 * @since 2024-11-07 16:49:19
*/ */
@Service @Service
@Transactional
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService { public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {
@Autowired @Autowired

View File

@ -1,3 +1,5 @@
server:
port: 1010
logging: logging:
level: level:
cn.bunny.service.mapper: info cn.bunny.service.mapper: info
@ -43,5 +45,4 @@ bunny:
secretKey: "02120212" secretKey: "02120212"
bucket-name: financial bucket-name: financial
backPath: "D:\\MyData\\backup"
bashPath: "D:\\MyData" bashPath: "D:\\MyData"

View File

@ -1,6 +1,3 @@
server:
port: 1000
spring: spring:
profiles: profiles:
active: @profiles.active@ active: @profiles.active@