add equipment parameter

This commit is contained in:
wangbin3 2021-06-29 19:58:54 +08:00
parent be5dc797d4
commit b0c465749b
2 changed files with 23 additions and 2 deletions

View File

@ -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));
}

View File

@ -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;
}