mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-29 18:51:53 +08:00
【代码优化】SYSTEM:操作日志,增加异步记录
This commit is contained in:
parent
424922b488
commit
56ae4503a6
@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
|||||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||||
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
|
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
|
||||||
import com.baomidou.mybatisplus.extension.incrementer.*;
|
import com.baomidou.mybatisplus.extension.incrementer.*;
|
||||||
|
import com.baomidou.mybatisplus.extension.parser.JsqlParserGlobal;
|
||||||
|
import com.baomidou.mybatisplus.extension.parser.cache.JdkSerialCaffeineJsqlParseCache;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
@ -16,6 +18,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MyBaits 配置类
|
* MyBaits 配置类
|
||||||
*
|
*
|
||||||
@ -26,6 +30,14 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
|||||||
lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载,目前仅用于单元测试
|
lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载,目前仅用于单元测试
|
||||||
public class YudaoMybatisAutoConfiguration {
|
public class YudaoMybatisAutoConfiguration {
|
||||||
|
|
||||||
|
static {
|
||||||
|
// 动态 SQL 智能优化支持本地缓存加速解析,更完善的租户复杂 XML 动态 SQL 支持,静态注入缓存
|
||||||
|
JsqlParserGlobal.setJsqlParseCache(new JdkSerialCaffeineJsqlParseCache(
|
||||||
|
(cache) -> cache.maximumSize(1024)
|
||||||
|
.expireAfterWrite(5, TimeUnit.SECONDS))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||||
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
|
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
|
||||||
|
@ -69,7 +69,7 @@ public class ApiSignatureAspect {
|
|||||||
|
|
||||||
// 3. 将 nonce 记入缓存,防止重复使用(重点二:此处需要将 ttl 设定为允许 timestamp 时间差的值 x 2 )
|
// 3. 将 nonce 记入缓存,防止重复使用(重点二:此处需要将 ttl 设定为允许 timestamp 时间差的值 x 2 )
|
||||||
String nonce = request.getHeader(signature.nonce());
|
String nonce = request.getHeader(signature.nonce());
|
||||||
signatureRedisDAO.setNonce(nonce, signature.timeout() * 2, signature.timeUnit());
|
signatureRedisDAO.setNonce(appId, nonce, signature.timeout() * 2, signature.timeUnit());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +113,7 @@ public class ApiSignatureAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. 检查 nonce 是否存在,有且仅能使用一次
|
// 3. 检查 nonce 是否存在,有且仅能使用一次
|
||||||
return signatureRedisDAO.getNonce(nonce) == null;
|
return signatureRedisDAO.getNonce(appId, nonce) == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -166,4 +166,3 @@ public class ApiSignatureAspect {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ public class ApiSignatureRedisDAO {
|
|||||||
* VALUE 格式:String
|
* VALUE 格式:String
|
||||||
* 过期时间:不固定
|
* 过期时间:不固定
|
||||||
*/
|
*/
|
||||||
private static final String SIGNATURE_NONCE = "api_signature_nonce:%s";
|
private static final String SIGNATURE_NONCE = "api_signature_nonce:%s:%s";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 签名密钥
|
* 签名密钥
|
||||||
@ -36,16 +36,16 @@ public class ApiSignatureRedisDAO {
|
|||||||
|
|
||||||
// ========== 验签随机数 ==========
|
// ========== 验签随机数 ==========
|
||||||
|
|
||||||
public String getNonce(String nonce) {
|
public String getNonce(String appId, String nonce) {
|
||||||
return stringRedisTemplate.opsForValue().get(formatNonceKey(nonce));
|
return stringRedisTemplate.opsForValue().get(formatNonceKey(appId, nonce));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNonce(String nonce, int time, TimeUnit timeUnit) {
|
public void setNonce(String appId, String nonce, int time, TimeUnit timeUnit) {
|
||||||
stringRedisTemplate.opsForValue().set(formatNonceKey(nonce), "", time, timeUnit);
|
stringRedisTemplate.opsForValue().set(formatNonceKey(appId, nonce), "", time, timeUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatNonceKey(String key) {
|
private static String formatNonceKey(String appId, String nonce) {
|
||||||
return String.format(SIGNATURE_NONCE, key);
|
return String.format(SIGNATURE_NONCE, appId, nonce);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== 签名密钥 ==========
|
// ========== 签名密钥 ==========
|
||||||
|
@ -69,7 +69,7 @@ public class ApiSignatureTest {
|
|||||||
// 断言结果
|
// 断言结果
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
// 断言调用
|
// 断言调用
|
||||||
verify(signatureRedisDAO).setNonce(eq(nonce), eq(120), eq(TimeUnit.SECONDS));
|
verify(signatureRedisDAO).setNonce(eq(appId), eq(nonce), eq(120), eq(TimeUnit.SECONDS));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -11,6 +11,7 @@ import com.mzt.logapi.service.ILogRecordService;
|
|||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -28,9 +29,10 @@ public class LogRecordServiceImpl implements ILogRecordService {
|
|||||||
private OperateLogApi operateLogApi;
|
private OperateLogApi operateLogApi;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Async
|
||||||
public void record(LogRecord logRecord) {
|
public void record(LogRecord logRecord) {
|
||||||
// 1. 补全通用字段
|
|
||||||
OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO();
|
OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO();
|
||||||
|
try {
|
||||||
reqDTO.setTraceId(TracerUtils.getTraceId());
|
reqDTO.setTraceId(TracerUtils.getTraceId());
|
||||||
// 补充用户信息
|
// 补充用户信息
|
||||||
fillUserFields(reqDTO);
|
fillUserFields(reqDTO);
|
||||||
@ -41,6 +43,10 @@ public class LogRecordServiceImpl implements ILogRecordService {
|
|||||||
|
|
||||||
// 2. 异步记录日志
|
// 2. 异步记录日志
|
||||||
operateLogApi.createOperateLog(reqDTO);
|
operateLogApi.createOperateLog(reqDTO);
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
// 由于 @Async 异步调用,这里打印下日志,更容易跟进
|
||||||
|
log.error("[record][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void fillUserFields(OperateLogCreateReqDTO reqDTO) {
|
private static void fillUserFields(OperateLogCreateReqDTO reqDTO) {
|
||||||
|
@ -63,6 +63,7 @@ public class BpmModelServiceImpl implements BpmModelService {
|
|||||||
@Override
|
@Override
|
||||||
public PageResult<Model> getModelPage(BpmModelPageReqVO pageVO) {
|
public PageResult<Model> getModelPage(BpmModelPageReqVO pageVO) {
|
||||||
ModelQuery modelQuery = repositoryService.createModelQuery();
|
ModelQuery modelQuery = repositoryService.createModelQuery();
|
||||||
|
modelQuery.modelTenantId(FlowableUtils.getTenantId());
|
||||||
if (StrUtil.isNotBlank(pageVO.getKey())) {
|
if (StrUtil.isNotBlank(pageVO.getKey())) {
|
||||||
modelQuery.modelKey(pageVO.getKey());
|
modelQuery.modelKey(pageVO.getKey());
|
||||||
}
|
}
|
||||||
@ -78,7 +79,6 @@ public class BpmModelServiceImpl implements BpmModelService {
|
|||||||
return PageResult.empty(count);
|
return PageResult.empty(count);
|
||||||
}
|
}
|
||||||
List<Model> models = modelQuery
|
List<Model> models = modelQuery
|
||||||
.modelTenantId(FlowableUtils.getTenantId())
|
|
||||||
.orderByCreateTime().desc()
|
.orderByCreateTime().desc()
|
||||||
.listPage(PageUtils.getStart(pageVO), pageVO.getPageSize());
|
.listPage(PageUtils.getStart(pageVO), pageVO.getPageSize());
|
||||||
return new PageResult<>(models, count);
|
return new PageResult<>(models, count);
|
||||||
|
@ -97,7 +97,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
|
|||||||
}
|
}
|
||||||
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1]));
|
taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
|
||||||
}
|
}
|
||||||
long count = taskQuery.count();
|
long count = taskQuery.count();
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
@ -119,7 +119,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
|
|||||||
}
|
}
|
||||||
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1]));
|
taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
|
||||||
}
|
}
|
||||||
// 执行查询
|
// 执行查询
|
||||||
long count = taskQuery.count();
|
long count = taskQuery.count();
|
||||||
@ -141,7 +141,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
|
|||||||
}
|
}
|
||||||
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
|
||||||
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1]));
|
taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
|
||||||
}
|
}
|
||||||
// 执行查询
|
// 执行查询
|
||||||
long count = taskQuery.count();
|
long count = taskQuery.count();
|
||||||
|
Loading…
Reference in New Issue
Block a user