fix(log): 修复设备类接口无用户上下文时操作日志报错

- 新增兜底操作人ID常量(99L)和X-Device-SN头解析
- 无登录用户时使用兜底ID和设备SN标识操作来源
- 调整文件上传临时目录为/tmp/ttstd/uploads
This commit is contained in:
TongTongStudio
2026-08-04 21:56:45 +08:00
parent 6614601519
commit 5915da6517
2 changed files with 20 additions and 3 deletions

View File

@@ -54,6 +54,13 @@ public class LogAspect {
public void pointcut(Log logAnnotation) {
}
/**
* 无用户上下文(如设备通过 X-Device-SN 上报时的兜底操作人ID。
* 设备类接口没有登录会话operatorId 无法获取,用该常量保证日志可落库。
*/
private static final long UNKNOWN_OPERATOR_ID = 99L;
private static final String DEVICE_SN_HEADER = "X-Device-SN";
@Around(value = "pointcut(logAnnotation)", argNames = "jp,logAnnotation")
public Object around(ProceedingJoinPoint jp, Log logAnnotation) throws Throwable {
long start = System.currentTimeMillis();
@@ -101,8 +108,10 @@ public class LogAspect {
entity.setActionType(action);
entity.setTitle(StrUtil.blankToDefault(ann.title(), module.getLabel() + "-" + action.getLabel()));
entity.setContent(ann.content());
entity.setOperatorId(userId);
entity.setOperatorName(username);
// 设备类接口无登录用户userId 为 null兜底为常量为保证日志可落库
entity.setOperatorId(userId != null ? userId : UNKNOWN_OPERATOR_ID);
// 无用户时尝试用设备SN标识操作来源
entity.setOperatorName(username != null ? username : resolveDeviceSn(req));
entity.setRequestUri(req.getRequestURI());
entity.setRequestMethod(req.getMethod());
entity.setIp(ip);
@@ -146,6 +155,14 @@ public class LogAspect {
return Optional.ofNullable(ua).map(UserAgent::getBrowser).map(b -> b.getName()).orElse(null);
}
/**
* 从设备上报请求头解析设备SN用于无登录用户时标识操作来源。
*/
private String resolveDeviceSn(HttpServletRequest req) {
String sn = req.getHeader(DEVICE_SN_HEADER);
return StrUtil.isBlank(sn) ? null : "设备-" + sn;
}
private record IpRegion(String province, String city) {
static final IpRegion EMPTY = new IpRegion(null, null);
}

View File

@@ -7,7 +7,7 @@ import java.io.File;
public class FilePath {
// @Value("${file.upload-dir-unix}")
private static final String unixUploadDir = "/data/uploads" ;
private static final String unixUploadDir = "/tmp/ttstd/uploads" ;
// @Value("${file.upload-dir-windows}")
private static final String windowsUploadDir = "uploadFile";