fix(logout): 登出成功事件处理中请求耗时计算错误

This commit is contained in:
carl 2024-12-01 10:52:04 +08:00
parent e6b922eff0
commit 42fc9204bf

View File

@ -16,7 +16,9 @@
package com.pig4cloud.pig.auth.support.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.common.core.util.WebUtils;
import com.pig4cloud.pig.common.log.event.SysLogEvent;
@ -57,19 +59,25 @@ public class PigLogoutSuccessEventHandler implements ApplicationListener<LogoutS
log.info("用户:{} 退出成功", authentication.getPrincipal());
SysLog logVo = SysLogUtils.getSysLog();
logVo.setTitle("退出成功");
// 发送异步日志事件
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
// 设置对应的token
WebUtils.getRequest().ifPresent(request -> logVo.setParams(request.getHeader(HttpHeaders.AUTHORIZATION)));
WebUtils.getRequest().ifPresent(request -> {
logVo.setParams(request.getHeader(HttpHeaders.AUTHORIZATION));
// 计算请求耗时
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
}
});
// 这边设置ServiceId
if (authentication instanceof PreAuthenticatedAuthenticationToken) {
logVo.setServiceId(authentication.getCredentials().toString());
}
logVo.setCreateBy(authentication.getName());
// 发送异步日志事件
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
}