🔨 优化操作日志逻辑.

This commit is contained in:
lijiahangmax
2025-10-24 11:18:54 +08:00
parent 91fecad956
commit 90705781f2
9 changed files with 106 additions and 5 deletions

View File

@@ -33,6 +33,20 @@ import java.io.Serializable;
*/
public interface RequestIdentity extends Serializable {
/**
* 获取请求时间戳
*
* @return timestamp
*/
Long getTimestamp();
/**
* 设置请求时间戳
*
* @param timestamp timestamp
*/
void setTimestamp(Long timestamp);
/**
* 获取请求地址
*

View File

@@ -23,7 +23,10 @@
package org.dromara.visor.common.entity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 请求留痕模型
@@ -33,9 +36,15 @@ import lombok.Data;
* @since 2023/12/29 11:57
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(name = "RequestIdentityModel", description = "请求留痕模型")
public class RequestIdentityModel implements RequestIdentity {
@Schema(description = "时间戳")
private Long timestamp;
@Schema(description = "请求地址")
private String address;

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.common.mapstruct;
import java.util.Date;
/**
* date 转换器
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025/3/7 17:43
*/
public class DateConversion {
private DateConversion() {
}
/**
* Long > Date
*
* @param timestamp timestamp
* @return Date
*/
public static Date longToDate(Long timestamp) {
return timestamp != null ? new Date(timestamp) : null;
}
/**
* Date > Long
*
* @param date date
* @return Long
*/
public static Long dateToLong(Date date) {
return date != null ? date.getTime() : null;
}
}

View File

@@ -64,10 +64,24 @@ public class Requests {
.map(ServletRequestAttributes::getRequest)
.ifPresent(request -> {
String address = IpUtils.getRemoteAddr(request);
identity.setTimestamp(System.currentTimeMillis());
identity.setAddress(address);
identity.setLocation(IpUtils.getLocation(address));
identity.setUserAgent(Servlets.getUserAgent(request));
});
}
/**
* 复制留痕信息
*
* @param source source
* @param target target
*/
public static void copyIdentity(RequestIdentityModel source, RequestIdentityModel target) {
target.setTimestamp(source.getTimestamp());
target.setAddress(source.getAddress());
target.setLocation(source.getLocation());
target.setUserAgent(source.getUserAgent());
}
}

View File

@@ -123,8 +123,8 @@ public class OperatorLogModel implements RequestIdentity {
private Date endTime;
/**
* 创建时间
* 时间
*/
private Date createTime;
private Long timestamp;
}

View File

@@ -31,8 +31,8 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import org.dromara.visor.common.entity.RequestIdentity;
import org.dromara.visor.common.enums.BooleanBit;
import org.dromara.visor.common.trace.TraceIdHolder;
import org.dromara.visor.common.security.LoginUser;
import org.dromara.visor.common.trace.TraceIdHolder;
import org.dromara.visor.common.utils.Requests;
import org.dromara.visor.framework.biz.operator.log.configuration.config.OperatorLogConfig;
import org.dromara.visor.framework.biz.operator.log.core.enums.ReturnType;
@@ -91,6 +91,7 @@ public class OperatorLogFiller implements Gettable<OperatorLogModel> {
*/
public OperatorLogFiller fillUsedTime(long start) {
long end = System.currentTimeMillis();
model.setTimestamp(start);
model.setDuration((int) (end - start));
model.setStartTime(new Date(start));
model.setEndTime(new Date(end));

View File

@@ -24,6 +24,7 @@ package org.dromara.visor.framework.test.core.base;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.dromara.visor.common.configuration.CommonConfiguration;
import org.dromara.visor.common.configuration.SpringConfiguration;
import org.dromara.visor.framework.datasource.configuration.OrionDataSourceAutoConfiguration;
import org.dromara.visor.framework.mybatis.configuration.OrionMybatisAutoConfiguration;
@@ -60,6 +61,7 @@ public class BaseUnitTest {
@Import({
// spring
SpringConfiguration.class,
CommonConfiguration.class,
// mock
OrionMockBeanTestConfiguration.class,
OrionMockRedisTestConfiguration.class,

View File

@@ -22,12 +22,14 @@
*/
package org.dromara.visor.module.infra.convert;
import org.dromara.visor.common.mapstruct.DateConversion;
import org.dromara.visor.framework.biz.operator.log.core.model.OperatorLogModel;
import org.dromara.visor.module.infra.entity.domain.OperatorLogDO;
import org.dromara.visor.module.infra.entity.request.operator.OperatorLogQueryRequest;
import org.dromara.visor.module.infra.entity.vo.LoginHistoryVO;
import org.dromara.visor.module.infra.entity.vo.OperatorLogVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
@@ -37,11 +39,12 @@ import org.mapstruct.factory.Mappers;
* @version 1.0.0
* @since 2023-10-10 17:08
*/
@Mapper
@Mapper(uses = DateConversion.class)
public interface OperatorLogConvert {
OperatorLogConvert MAPPER = Mappers.getMapper(OperatorLogConvert.class);
@Mapping(source = "timestamp", target = "createTime")
OperatorLogDO to(OperatorLogModel model);
OperatorLogDO to(OperatorLogQueryRequest request);

View File

@@ -50,7 +50,6 @@ public class TerminalAsyncSaver {
*/
public static void saveOperatorLog(OperatorLogModel model) {
TerminalThreadPools.TERMINAL_ASYNC_SAVER.execute(() -> {
model.setCreateTime(model.getStartTime());
operatorLogFrameworkService.insert(model);
});
}