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.ServletRequestAttributes;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@ -30,6 +32,18 @@ public class UserRequestLogAop {
@Autowired
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)")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
LoginVo loginVo = BaseContext.getLoginVo();
@ -52,8 +66,9 @@ public class UserRequestLogAop {
userRequestLog.setIpRegion(ipRegion);
userRequestLog.setExecuteTime(executeTime);
userRequestLog.setArg(JSON.toJSONString(args));
// 判断传入参数能否被序列化
Object saveArgs = isSerializable(args) ? JSON.toJSONString(args) : "";
userRequestLog.setArg(JSON.toJSONString(saveArgs));
// 当前请求request
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.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@ -60,6 +61,7 @@ import java.util.stream.Collectors;
* @since 2024-11-07 16:49:19
*/
@Service
@Transactional
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements BillService {
@Autowired

View File

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

View File

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