fix(log): 修复设备类接口无用户上下文时操作日志报错
- 新增兜底操作人ID常量(99L)和X-Device-SN头解析 - 无登录用户时使用兜底ID和设备SN标识操作来源 - 调整文件上传临时目录为/tmp/ttstd/uploads
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user