diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java index 290d72d6c..8087197ed 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java @@ -17,7 +17,6 @@ package com.pig4cloud.pigx.auth.handler; -import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.admin.api.entity.SysLog; import com.pig4cloud.pigx.admin.api.feign.RemoteLogService; import com.pig4cloud.pigx.common.core.constant.CommonConstants; @@ -67,9 +66,7 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail sysLog.setParams(username); sysLog.setException(authenticationException.getLocalizedMessage()); String header = request.getHeader(HttpHeaders.AUTHORIZATION); - if (StrUtil.isNotBlank(header)) { - sysLog.setServiceId(WebUtils.getClientId(header)); - } + sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A")); logService.saveLog(sysLog, SecurityConstants.FROM_IN); diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java index c8daca597..d3e5dfbc6 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java @@ -17,7 +17,6 @@ package com.pig4cloud.pigx.auth.handler; -import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.admin.api.entity.SysLog; import com.pig4cloud.pigx.admin.api.feign.RemoteLogService; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; @@ -61,9 +60,7 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc sysLog.setTitle(username + "用户登录"); sysLog.setParams(username); String header = request.getHeader(HttpHeaders.AUTHORIZATION); - if (StrUtil.isNotBlank(header)) { - sysLog.setServiceId(WebUtils.getClientId(header)); - } + sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A")); logService.saveLog(sysLog, SecurityConstants.FROM_IN); log.info("用户:{} 登录成功", username); diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java index 6e2bf43a0..cd380d483 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java @@ -23,7 +23,6 @@ import cn.hutool.core.codec.Base64; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.pig4cloud.pigx.common.core.exception.CheckedException; -import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; @@ -40,6 +39,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; +import java.util.Optional; /** @@ -205,23 +205,24 @@ public class WebUtils extends org.springframework.web.util.WebUtils { } /** - * 从request 获取CLIENT_ID - * - * @return + * 解析 client id + * @param header + * @param defVal + * @return 如果解析失败返回默认值 */ - @SneakyThrows - public String getClientId(String header) { + public String extractClientId(String header,final String defVal) { if (header == null || !header.startsWith(BASIC_)) { - throw new CheckedException("请求头中client信息为空"); + log.debug("请求头中client信息为空: {}",header); + return defVal; } - byte[] base64Token = header.substring(6).getBytes("UTF-8"); + byte[] base64Token = header.substring(6).getBytes(StandardCharsets.UTF_8); byte[] decoded; try { decoded = Base64.decode(base64Token); } catch (IllegalArgumentException e) { - throw new CheckedException( - "Failed to decode basic authentication token"); + log.debug("Failed to decode basic authentication token: {}",header); + return defVal; } String token = new String(decoded, StandardCharsets.UTF_8); @@ -229,9 +230,30 @@ public class WebUtils extends org.springframework.web.util.WebUtils { int delim = token.indexOf(":"); if (delim == -1) { - throw new CheckedException("Invalid basic authentication token"); + log.debug("Invalid basic authentication token: {}",header); + return defVal; } return token.substring(0, delim); } + /** + * 从请求头中解析 client id + * @param header + * @return + */ + public Optional extractClientId(String header){ + return Optional.ofNullable(extractClientId(header,null)); + } + /** + * 从request 获取CLIENT_ID + * + * @return + */ + public String getClientId(String header) { + String clientId = extractClientId(header,null); + if(clientId == null){ + throw new CheckedException("Invalid basic authentication token"); + } + return clientId; + } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java index aa0e8ee5a..d323ca3db 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java @@ -19,11 +19,11 @@ package com.pig4cloud.pigx.common.security.listener; import cn.hutool.core.collection.CollUtil; import com.pig4cloud.pigx.common.security.handler.AuthenticationSuccessHandler; +import com.pig4cloud.pigx.common.security.service.PigxUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.security.authentication.event.AuthenticationSuccessEvent; import org.springframework.security.core.Authentication; -import org.springframework.security.core.userdetails.User; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -58,7 +58,7 @@ public class AuthenticationSuccessEventListener implements ApplicationListener