添加操作日志
This commit is contained in:
@@ -11,6 +11,7 @@ import org.springframework.core.Ordered;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.app.common.cache.enums.AppCache;
|
||||
import xtools.app.sys.auth.model.dto.LoginUserDto;
|
||||
import xtools.app.sys.auth.model.dto.OptLogDto;
|
||||
import xtools.app.sys.auth.utils.AuthUtils;
|
||||
import xtools.app.sys.auth.utils.PremUtils;
|
||||
import xtools.app.sys.auth.whitelist.AuthWhitelist;
|
||||
@@ -22,14 +23,18 @@ import xtools.boot.cache.redis.base.RedisService;
|
||||
import xtools.boot.core.holder.CommonHolder;
|
||||
import xtools.boot.core.utils.PathPatternUtils;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.boot.mask.utils.MaskIgnoreUtils;
|
||||
import xtools.boot.web.filter.base.BaseFilter;
|
||||
import xtools.core.CollectionUtils;
|
||||
import xtools.core.StringUtils;
|
||||
import xtools.core.enums.LogLevel;
|
||||
import xtools.web.HeaderUtils;
|
||||
import xtools.web.HttpServletUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
@@ -47,7 +52,7 @@ import java.util.Set;
|
||||
* @date : 2026/1/31 21:18
|
||||
*/
|
||||
@Component
|
||||
public class AuthFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
public class AuthFilter extends BaseFilter implements Ordered, BootCommonConstant, BaseParams {
|
||||
|
||||
/**
|
||||
* 微服务标识
|
||||
@@ -157,6 +162,10 @@ public class AuthFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
) throws ServletException, IOException {
|
||||
// 获取访问 uri
|
||||
String uri = HeaderUtils.getUri(request);
|
||||
String ip = HeaderUtils.getIp(request);
|
||||
OptLogDto log = new OptLogDto();
|
||||
log.setUri(uri);
|
||||
log.setIp(ip);
|
||||
|
||||
// 忽略权限校验
|
||||
if (checkAuthWhiteList(uri)) {
|
||||
@@ -184,11 +193,13 @@ public class AuthFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
HttpServletUtils.respWriter(response, JSONObject.from(new Result<>(ResultType.UNAUTHORIZED, null)));
|
||||
return;
|
||||
}
|
||||
|
||||
log.setAccountId(loginUser.getId());
|
||||
log.setAccount(loginUser.getAccount());
|
||||
|
||||
// 校验 uri 访问权限
|
||||
String method = request.getMethod();
|
||||
if (!PremUtils.checkInterfacePerm(uri, method, loginUser.getRoleIds())) {
|
||||
saveOptLog(log, "URI访问权限校验失败");
|
||||
HttpServletUtils.respWriter(response, JSONObject.from(new Result<>(ResultType.FORBIDDEN, null)));
|
||||
return;
|
||||
}
|
||||
@@ -199,7 +210,27 @@ public class AuthFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
|
||||
// 校验忽略掩码
|
||||
checkIgnoreMask();
|
||||
CommonHolder.set(GET_SWAGGER_TAG, true);
|
||||
filterChain.doFilter(request, response);
|
||||
saveOptLog(log, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存操作日志
|
||||
*
|
||||
* @param log 日志
|
||||
* @param memo 备注
|
||||
*/
|
||||
private void saveOptLog(OptLogDto log, String memo) {
|
||||
Object tag = CommonHolder.get(SWAGGER_TAG);
|
||||
if (Objects.nonNull(tag)) {
|
||||
{
|
||||
log.setTitle(tag.toString());
|
||||
}
|
||||
}
|
||||
log.setMemo(memo);
|
||||
log.setGmtCreate(Instant.now());
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.OPT_LOG).data(log).save();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package xtools.app.sys.auth.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
|
||||
|
||||
/**
|
||||
* <p>Title : OptLogDto</p>
|
||||
* <p>Description : OptLogDto</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 1.0.0
|
||||
* @date : 2026/6/2 17:28
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class OptLogDto implements Serializable {
|
||||
|
||||
/**
|
||||
* 日志追踪id
|
||||
*/
|
||||
@Schema(description = "日志追踪id")
|
||||
private String traceId;
|
||||
|
||||
/**
|
||||
* 标题
|
||||
*/
|
||||
@Schema(description = "标题")
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 账号ID
|
||||
*/
|
||||
@Schema(description = "账号ID")
|
||||
private Long accountId;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@Schema(description = "账号")
|
||||
private String account;
|
||||
|
||||
/**
|
||||
* IP
|
||||
*/
|
||||
@Schema(description = "IP")
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 操作URI
|
||||
*/
|
||||
@Schema(description = "操作URI")
|
||||
private String uri;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
@Schema(description = "备注")
|
||||
private String memo;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间", example = "2026-01-05 10:32:00")
|
||||
private Instant gmtCreate;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user