From b0c465749b7293282ef08eda0375ce10a2ad3152 Mon Sep 17 00:00:00 2001 From: wangbin3 Date: Tue, 29 Jun 2021 19:58:54 +0800 Subject: [PATCH] add equipment parameter --- .../handler/PigLogoutSuccessEventHandler.java | 6 ++++++ .../pig/common/log/util/SysLogUtils.java | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/handler/PigLogoutSuccessEventHandler.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/handler/PigLogoutSuccessEventHandler.java index 95633f14..50edc245 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/handler/PigLogoutSuccessEventHandler.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/handler/PigLogoutSuccessEventHandler.java @@ -23,6 +23,7 @@ import com.pig4cloud.pig.common.log.util.SysLogUtils; import com.pig4cloud.pig.common.security.handler.AbstractLogoutSuccessEventHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails; import org.springframework.stereotype.Component; @@ -52,6 +53,11 @@ public class PigLogoutSuccessEventHandler extends AbstractLogoutSuccessEventHand Long startTime = System.currentTimeMillis(); Long endTime = System.currentTimeMillis(); logVo.setTime(endTime - startTime); + // 这边设置ServiceId + if (authentication instanceof OAuth2Authentication) { + OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication; + logVo.setServiceId(auth2Authentication.getOAuth2Request().getClientId()); + } SpringContextHolder.publishEvent(new SysLogEvent(logVo)); } diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java index b9992db3..c45808a4 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java @@ -16,12 +16,14 @@ package com.pig4cloud.pig.common.log.util; +import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.http.HttpUtil; import com.pig4cloud.pig.admin.api.entity.SysLog; import lombok.experimental.UtilityClass; import org.springframework.http.HttpHeaders; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.provider.OAuth2Authentication; @@ -29,7 +31,10 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; +import java.nio.charset.Charset; +import java.util.Base64; import java.util.Objects; +import java.util.Optional; /** * 系统日志工具类 @@ -50,7 +55,7 @@ public class SysLogUtils { sysLog.setMethod(request.getMethod()); sysLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); sysLog.setParams(HttpUtil.toParams(request.getParameterMap())); - sysLog.setServiceId(getClientId()); + sysLog.setServiceId(getClientId(request)); return sysLog; } @@ -58,12 +63,22 @@ public class SysLogUtils { * 获取客户端 * @return clientId */ - private String getClientId() { + private String getClientId(HttpServletRequest request) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication instanceof OAuth2Authentication) { OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication; return auth2Authentication.getOAuth2Request().getClientId(); } + if (authentication instanceof UsernamePasswordAuthenticationToken){ + // 通过请求参数拿到clientId + String authorizationHeaderValue = request.getHeader("Authorization"); + String base64AuthorizationHeader = Optional.ofNullable(authorizationHeaderValue) + .map(headerValue->headerValue.substring("Basic ".length())).orElse(""); + if(StrUtil.isNotEmpty(base64AuthorizationHeader)) { + String decodedAuthorizationHeader = new String(Base64.getDecoder().decode(base64AuthorizationHeader), Charset.forName("UTF-8")); + return decodedAuthorizationHeader.split(":")[0]; + } + } return null; }