♻️ Refactoring code. 优化登录日志记录时间

This commit is contained in:
lbw committed 2022-08-21 11:40:15 +08:00
1 parent ef57c77719
commit a77bc1a099
7 files changed
+53 -3

No files matched your search

@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
import com.pig4cloud.pigx.common.core.util.WebUtils;
@@ -65,6 +67,13 @@ public class PigxAuthenticationFailureLogEventHandler implements AuthenticationF
HttpServletRequest request, HttpServletResponse response) {
String username = authentication.getName();
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
sysLog.setTitle(username + "用户登录");
sysLog.setLogType(LogTypeEnum.ERROR.getType());
sysLog.setParams(username);
@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.log.util.SysLogUtils;
import com.pig4cloud.pigx.common.security.handler.AuthenticationLogoutHandler;
@@ -64,6 +66,13 @@ public class PigxAuthenticationLogoutEventHandler implements AuthenticationLogou
sysLog.setTitle(pigxUser.getUsername() + "用户登录");
sysLog.setParams(pigxUser.getUsername());
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
// 获取clientId 信息
OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
sysLog.setServiceId(auth2Authentication.getOAuth2Request().getClientId());
@@ -17,8 +17,10 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.dto.SysLogDTO;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.KeyStrResolver;
import com.pig4cloud.pigx.common.core.util.WebUtils;
@@ -64,6 +66,14 @@ public class PigxAuthenticationSuccessLogEventHandler implements AuthenticationS
SysLogDTO sysLog = SysLogUtils.getSysLog(request, username);
sysLog.setTitle(username + "用户登录");
sysLog.setParams(username);
String startTimeStr = request.getHeader(CommonConstants.REQUEST_START_TIME);
if (StrUtil.isNotBlank(startTimeStr)) {
Long startTime = Long.parseLong(startTimeStr);
Long endTime = System.currentTimeMillis();
sysLog.setTime(endTime - startTime);
}
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
sysLog.setTenantId(Long.parseLong(tenantKeyStrResolver.key()));