解决文件预览失败的日志记录。

This commit is contained in:
chenxudong 2025-03-25 09:16:53 +08:00
parent 228a8d5142
commit ff32992c8d
5 changed files with 101 additions and 48 deletions

View File

@ -0,0 +1,47 @@
package com.electromagnetic.industry.software.manage.aop;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class AopUtil {
public static String getReqArgs(Object[] args, String[] argNames) {
String paramInfo = "";
if (args != null && args.length > 0) {
Map<String, Object> map = new HashMap<>();
try {
for (int i = 0; i < args.length; i++) {
String name = argNames[i];
Object value = args[i];
if (value instanceof ServletRequest) {
log.info("参数中有request");
map.put(name, "request");
} else if (value instanceof ServletResponse) {
log.info("参数中有response");
map.put(name, "response");
} else if (value instanceof MultipartFile) {
MultipartFile file = (MultipartFile) value;
Map<String, Object> pars = new HashMap<>();
pars.put("fileName", file.getOriginalFilename());
pars.put("fileSize", file.getSize());
map.put("file", pars);
} else {
map.put(name, value);
}
}
} catch (Exception e) {
log.warn("切面异常--->{}", e.getMessage(), e);
} finally {
paramInfo = JSONUtil.toJsonStr(map);
}
}
return paramInfo;
}
}

View File

@ -12,12 +12,15 @@ import com.electromagnetic.industry.software.manage.service.PermissionService;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.junit.jupiter.api.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@Aspect @Aspect
@Component @Component
@Order(1)
public class FilePermissionCheckAspect { public class FilePermissionCheckAspect {
@Resource @Resource
@ -28,7 +31,9 @@ public class FilePermissionCheckAspect {
@Around("@annotation(requiredPermission)") @Around("@annotation(requiredPermission)")
public Object requirePermission(ProceedingJoinPoint joinPoint, RequiredPermission requiredPermission) throws Throwable { public Object requirePermission(ProceedingJoinPoint joinPoint, RequiredPermission requiredPermission) throws Throwable {
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
String paramInfo = AopUtil.getReqArgs(args, argNames);
UserThreadLocal.setReqArgs(paramInfo);
if (args.length == 0) { if (args.length == 0) {
return joinPoint.proceed(); return joinPoint.proceed();
} }
@ -36,7 +41,6 @@ public class FilePermissionCheckAspect {
FilePermission filePermission = requiredPermission.value(); FilePermission filePermission = requiredPermission.value();
String userId = UserThreadLocal.getUserId(); String userId = UserThreadLocal.getUserId();
String id = extractId(args[0]); // 提取ID逻辑封装成方法减少冗余代码 String id = extractId(args[0]); // 提取ID逻辑封装成方法减少冗余代码
// 特殊处理 FileInfoQueryDTO // 特殊处理 FileInfoQueryDTO
if (args[0] instanceof FileInfoQueryDTO) { if (args[0] instanceof FileInfoQueryDTO) {
if (id.length() > ElectromagneticConstants.PRJ_ID_LENGTH) { if (id.length() > ElectromagneticConstants.PRJ_ID_LENGTH) {

View File

@ -10,20 +10,17 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.junit.jupiter.api.Order;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch; import org.springframework.util.StopWatch;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
@Aspect @Aspect
@Component @Component
@Slf4j @Slf4j
@Order(0)
public class ServiceAspect { public class ServiceAspect {
/** /**
@ -35,35 +32,7 @@ public class ServiceAspect {
+ jp.getSignature().getName(); + jp.getSignature().getName();
Object[] args = jp.getArgs(); Object[] args = jp.getArgs();
String[] argNames = ((MethodSignature) jp.getSignature()).getParameterNames(); String[] argNames = ((MethodSignature) jp.getSignature()).getParameterNames();
String paramInfo = ""; String paramInfo = AopUtil.getReqArgs(args, argNames);
if (args != null && args.length > 0) {
Map<String, Object> map = new HashMap<>();
try {
for (int i = 0; i < args.length; i++) {
String name = argNames[i];
Object value = args[i];
if (value instanceof ServletRequest) {
log.info("参数中有request");
map.put(name, "request");
} else if (value instanceof ServletResponse) {
log.info("参数中有response");
map.put(name, "response");
} else if (value instanceof MultipartFile) {
MultipartFile file = (MultipartFile) value;
Map<String, Object> pars = new HashMap<>();
pars.put("fileName", file.getOriginalFilename());
pars.put("fileSize", file.getSize());
map.put("file", pars);
} else {
map.put(name, value);
}
}
} catch (Exception e) {
log.warn("切面异常--->{}", e.getMessage(), e);
} finally {
paramInfo = JSONUtil.toJsonStr(map);
}
}
log.info("请求接口开始:{},参数:{}", methodInfo, paramInfo); log.info("请求接口开始:{},参数:{}", methodInfo, paramInfo);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
StopWatch stopwatch = new StopWatch(); StopWatch stopwatch = new StopWatch();
@ -73,15 +42,12 @@ public class ServiceAspect {
UserThreadLocal.set(userLoginInfo); UserThreadLocal.set(userLoginInfo);
} }
UserThreadLocal.setReqArgs(paramInfo); UserThreadLocal.setReqArgs(paramInfo);
Object rvt = jp.proceed(); Object rvt = jp.proceed();
if (rvt instanceof ResponseEntity) { if (rvt instanceof ResponseEntity) {
UserThreadLocal.setRes(ElectromagneticResultUtil.success("")); UserThreadLocal.setRes(ElectromagneticResultUtil.success(""));
return rvt; return rvt;
} }
UserThreadLocal.setRes((ElectromagneticResult) rvt); UserThreadLocal.setRes((ElectromagneticResult) rvt);
String returnInfo = JSONUtil.toJsonStr(rvt); String returnInfo = JSONUtil.toJsonStr(rvt);
log.info("请求接口结束:{},返回参数:{},接口耗时:{}", methodInfo, returnInfo, (System.currentTimeMillis() - startTime) + "毫秒"); log.info("请求接口结束:{},返回参数:{},接口耗时:{}", methodInfo, returnInfo, (System.currentTimeMillis() - startTime) + "毫秒");
stopwatch.stop(); stopwatch.stop();

View File

@ -1,5 +1,6 @@
package com.electromagnetic.industry.software.manage.config; package com.electromagnetic.industry.software.manage.config;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.SystemClock; import cn.hutool.core.date.SystemClock;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
@ -137,7 +138,9 @@ public class LoginInterceptor implements HandlerInterceptor {
UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo()); UserLoginInfo user = Optional.of(UserThreadLocal.getUser()).orElse(new UserLoginInfo());
List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList(); List<AccessSuccessInfo> successInfoList = user.getSuccessInfoList();
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
if (CollUtil.isEmpty(successInfoList)) {
ElectromagneticResult res = user.getResult();
UserAccessLog userAccessLog = new UserAccessLog() UserAccessLog userAccessLog = new UserAccessLog()
.setId(IdWorker.getSnowFlakeIdString()) .setId(IdWorker.getSnowFlakeIdString())
.setUserId(user.getUserId()) .setUserId(user.getUserId())
@ -152,22 +155,54 @@ public class LoginInterceptor implements HandlerInterceptor {
.setAccessSuccess(true) .setAccessSuccess(true)
.setCreateTime(new Date()) .setCreateTime(new Date())
.setOperationModule(userOperation.modelName().key) .setOperationModule(userOperation.modelName().key)
.setOperationMsg(accessSuccessInfo.getSuccessMsg()) .setOperationMsg(res.getErrorMessage())
.setDataId(accessSuccessInfo.getDataId()) .setDataId("")
.setParentId(accessSuccessInfo.getParentId()); .setParentId("");
ElectromagneticResult<?> result = user.getResult(); if (res != null) {
if (result != null) { userAccessLog.setResponse(JSONUtil.toJsonStr(res));
userAccessLog.setResponse(JSONUtil.toJsonStr(result)); if (!res.getSuccess()) {
if (!result.getSuccess()) {
userAccessLog.setAccessSuccess(false); userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(result.getErrorMessage()); userAccessLog.setOperationMsg(res.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail()); userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
} }
} else { // 返回为ResponseEntity且状态为失败 } else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false); userAccessLog.setAccessSuccess(false);
} }
userAccessLogMapper.insert(userAccessLog); userAccessLogMapper.insert(userAccessLog);
} else {
for (AccessSuccessInfo accessSuccessInfo : successInfoList) {
UserAccessLog userAccessLog = new UserAccessLog()
.setId(IdWorker.getSnowFlakeIdString())
.setUserId(user.getUserId())
.setAccessStartTime(DateUtil.date(accessStartTime))
.setAccessEndTime(DateUtil.date(accessEndTime))
.setAccessDuration(accessEndTime - accessStartTime)
.setAction(userOperation.value())
.setRequestUrl(request.getRequestURL().toString())
.setRequestIp(parseIpFromUrl(request.getRequestURL().toString()))
.setReqArgs(reqArgs)
.setRemoteAddr(getRealIp(request))
.setAccessSuccess(true)
.setCreateTime(new Date())
.setOperationModule(userOperation.modelName().key)
.setOperationMsg(accessSuccessInfo.getSuccessMsg())
.setDataId(accessSuccessInfo.getDataId())
.setParentId(accessSuccessInfo.getParentId());
ElectromagneticResult<?> result = user.getResult();
if (result != null) {
userAccessLog.setResponse(JSONUtil.toJsonStr(result));
if (!result.getSuccess()) {
userAccessLog.setAccessSuccess(false);
userAccessLog.setOperationMsg(result.getErrorMessage());
userAccessLog.setExceptionDetail(UserThreadLocal.getUser().getExceptionDetail());
}
} else { // 返回为ResponseEntity且状态为失败
userAccessLog.setAccessSuccess(false);
}
userAccessLogMapper.insert(userAccessLog);
}
} }
UserThreadLocal.remove(); UserThreadLocal.remove();

View File

@ -1077,6 +1077,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
User singleUser = userMapper.getSingleUser(fileInfo.getCreatedBy()); User singleUser = userMapper.getSingleUser(fileInfo.getCreatedBy());
fileInfo.setCreatedBy(singleUser.getUserName()); fileInfo.setCreatedBy(singleUser.getUserName());
FileInfoVO fileInfoVO = BeanUtil.copyProperties(fileInfo, FileInfoVO.class); FileInfoVO fileInfoVO = BeanUtil.copyProperties(fileInfo, FileInfoVO.class);
fileInfoVO.setFileSizeShow(EleCommonUtil.convertFileSize(fileInfoVO.getFileSize()));
fileInfoVO.setLabels(fileTagRelationService.getFileTags(fileInfo.getFileId())); fileInfoVO.setLabels(fileTagRelationService.getFileTags(fileInfo.getFileId()));
UserThreadLocal.setSuccessInfo(fileInfo.getParentId(), id, "查询了文件的详细信息"); UserThreadLocal.setSuccessInfo(fileInfo.getParentId(), id, "查询了文件的详细信息");
return ElectromagneticResultUtil.success(fileInfoVO); return ElectromagneticResultUtil.success(fileInfoVO);