!132 update pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java.

Merge pull request !132 from Hacker/N/A
This commit is contained in:
lengleng 2021-07-13 09:17:53 +00:00 committed by Gitee
commit 09d8c3a2ce
1 changed files with 8 additions and 14 deletions

View File

@ -16,7 +16,6 @@
package com.pig4cloud.pig.common.log.util; package com.pig4cloud.pig.common.log.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil; 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.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication; 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.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
/** /**
* 系统日志工具类 * 系统日志工具类
@ -61,6 +58,7 @@ public class SysLogUtils {
/** /**
* 获取客户端 * 获取客户端
*
* @return clientId * @return clientId
*/ */
private String getClientId(HttpServletRequest request) { private String getClientId(HttpServletRequest request) {
@ -70,14 +68,10 @@ public class SysLogUtils {
return auth2Authentication.getOAuth2Request().getClientId(); return auth2Authentication.getOAuth2Request().getClientId();
} }
if (authentication instanceof UsernamePasswordAuthenticationToken) { if (authentication instanceof UsernamePasswordAuthenticationToken) {
// 通过请求参数拿到clientId BasicAuthenticationConverter basicAuthenticationConverter = new BasicAuthenticationConverter();
String authorizationHeaderValue = request.getHeader("Authorization"); UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = basicAuthenticationConverter.convert(request);
String base64AuthorizationHeader = Optional.ofNullable(authorizationHeaderValue) if (usernamePasswordAuthenticationToken != null) {
.map(headerValue -> headerValue.substring("Basic ".length())).orElse(""); return usernamePasswordAuthenticationToken.getName();
if (StrUtil.isNotEmpty(base64AuthorizationHeader)) {
String decodedAuthorizationHeader = new String(Base64.getDecoder().decode(base64AuthorizationHeader),
Charset.forName("UTF-8"));
return decodedAuthorizationHeader.split(":")[0];
} }
} }
return null; return null;
@ -85,6 +79,7 @@ public class SysLogUtils {
/** /**
* 获取用户名称 * 获取用户名称
*
* @return username * @return username
*/ */
private String getUsername() { private String getUsername() {
@ -94,5 +89,4 @@ public class SysLogUtils {
} }
return authentication.getName(); return authentication.getName();
} }
}
}