From 95d1dce4be2d13050791633faa9b02c22820370a Mon Sep 17 00:00:00 2001 From: Hacker <721806280@qq.com> Date: Tue, 13 Jul 2021 07:55:53 +0000 Subject: [PATCH] update pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java. --- .../pig/common/log/util/SysLogUtils.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) 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 ff0176e7..432554cd 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,7 +16,6 @@ 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; @@ -27,14 +26,12 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.web.authentication.www.BasicAuthenticationConverter; 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; /** * 系统日志工具类 @@ -61,6 +58,7 @@ public class SysLogUtils { /** * 获取客户端 + * * @return clientId */ private String getClientId(HttpServletRequest request) { @@ -70,14 +68,10 @@ public class SysLogUtils { 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]; + BasicAuthenticationConverter basicAuthenticationConverter = new BasicAuthenticationConverter(); + UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = basicAuthenticationConverter.convert(request); + if (usernamePasswordAuthenticationToken != null) { + return usernamePasswordAuthenticationToken.getName(); } } return null; @@ -85,6 +79,7 @@ public class SysLogUtils { /** * 获取用户名称 + * * @return username */ private String getUsername() { @@ -94,5 +89,4 @@ public class SysLogUtils { } return authentication.getName(); } - -} +} \ No newline at end of file