|
@@ -23,11 +23,14 @@ import cn.hutool.core.util.URLUtil;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import com.pig4cloud.pigx.admin.api.entity.SysLog;
|
|
|
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
|
|
|
-import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* 系统日志工具类
|
|
@@ -36,16 +39,45 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
*/
|
|
|
public class SysLogUtils {
|
|
|
public static SysLog getSysLog() {
|
|
|
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) Objects
|
|
|
+ .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
|
|
SysLog sysLog = new SysLog();
|
|
|
- sysLog.setCreateBy(SecurityUtils.getUser().getUsername());
|
|
|
+ sysLog.setCreateBy(Objects.requireNonNull(getUsername()));
|
|
|
sysLog.setType(CommonConstant.STATUS_NORMAL);
|
|
|
sysLog.setRemoteAddr(HttpUtil.getClientIP(request));
|
|
|
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
|
|
|
sysLog.setMethod(request.getMethod());
|
|
|
sysLog.setUserAgent(request.getHeader("user-agent"));
|
|
|
sysLog.setParams(HttpUtil.toParams(request.getParameterMap()));
|
|
|
- sysLog.setServiceId(SecurityUtils.getClientId());
|
|
|
+ sysLog.setServiceId(getClientId());
|
|
|
return sysLog;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取客户端
|
|
|
+ *
|
|
|
+ * @return clientId
|
|
|
+ */
|
|
|
+ private static String getClientId() {
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (authentication instanceof OAuth2Authentication) {
|
|
|
+ OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
|
|
|
+ return auth2Authentication.getOAuth2Request().getClientId();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户名称
|
|
|
+ *
|
|
|
+ * @return username
|
|
|
+ */
|
|
|
+ private static String getUsername() {
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (authentication == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return authentication.getName();
|
|
|
+ }
|
|
|
+
|
|
|
}
|