From 4e805cd792e90c251efbc831dfefe6561990ebf0 Mon Sep 17 00:00:00 2001 From: lbw Date: Fri, 17 Mar 2023 16:33:33 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=95=B0=E6=8D=AE=E5=BA=93=E8=AE=B0=E5=BD=95=20client?= =?UTF-8?q?=20=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/PigxAuthenticationFailureEventHandler.java | 5 +++++ .../handler/PigxAuthenticationSuccessEventHandler.java | 2 ++ .../java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java | 3 +++ 3 files changed, 10 insertions(+) diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java index 9e059ed36..95e4c5828 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java @@ -25,6 +25,7 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.util.KeyStrResolver; import com.pig4cloud.pigx.common.core.util.MsgUtils; import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.WebUtils; import com.pig4cloud.pigx.common.data.resolver.ParamResolver; import com.pig4cloud.pigx.common.log.event.SysLogEvent; import com.pig4cloud.pigx.common.log.util.LogTypeEnum; @@ -34,6 +35,7 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -117,6 +119,9 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail Long endTime = System.currentTimeMillis(); logVo.setTime(endTime - startTime); } + String header = request.getHeader(HttpHeaders.AUTHORIZATION); + String clientId = WebUtils.extractClientId(header).orElse(null); + logVo.setServiceId(clientId); logVo.setCreateBy(username); logVo.setTenantId(Long.parseLong(tenantKeyStrResolver.key())); publisher.publishEvent(new SysLogEvent(logVo)); diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java index 3983c9f2a..aad23a12c 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java @@ -117,6 +117,8 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc Long endTime = System.currentTimeMillis(); logVo.setTime(endTime - startTime); } + + logVo.setServiceId(accessTokenAuthentication.getRegisteredClient().getClientId()); logVo.setCreateBy(MapUtil.getStr(map, SecurityConstants.DETAILS_USERNAME)); logVo.setTenantId(Long.parseLong(tenantKeyStrResolver.key())); diff --git a/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java b/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java index 614960d8e..e68a135a7 100644 --- a/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java +++ b/pigx-common/pigx-common-log/src/main/java/com/pig4cloud/pigx/common/log/util/SysLogUtils.java @@ -70,6 +70,9 @@ public class SysLogUtils { */ private String getClientId() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null) { + return null; + } Object principal = authentication.getPrincipal(); if (principal instanceof OAuth2AuthenticatedPrincipal) {