From 7ed74fd04357aadf8c046fd2cd11af2a807da440 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?=
<15040126243@163.com>
Date: Thu, 15 Aug 2024 12:47:58 +0800
Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9spring=E6=BA=90=E7=A0=81=E4=B8=8A=E4=B8=8B=E6=96=87?=
=?UTF-8?q?=E6=8C=81=E6=9C=89=E8=80=85=20=E6=94=AF=E6=8C=81=E7=BA=BF?=
=?UTF-8?q?=E7=A8=8B=E5=88=87=E6=8D=A2=E4=BC=A0=E9=80=92=E4=B8=8A=E4=B8=8B?=
=?UTF-8?q?=E6=96=87=E6=95=B0=E6=8D=AE=20=E6=94=AF=E6=8C=81=E4=B8=80?=
=?UTF-8?q?=E5=88=87=E5=BC=82=E6=AD=A5=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?=
=?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=AD=89=E6=93=8D=E4=BD=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ruoyi-common/ruoyi-common-core/pom.xml | 7 +
.../context/request/RequestContextHolder.java | 161 ++++++++++++++++++
.../dromara/common/log/aspect/LogAspect.java | 13 --
.../common/log/event/LogEventListener.java | 14 ++
4 files changed, 182 insertions(+), 13 deletions(-)
create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/springframework/web/context/request/RequestContextHolder.java
diff --git a/ruoyi-common/ruoyi-common-core/pom.xml b/ruoyi-common/ruoyi-common-core/pom.xml
index 84ab453a..20c3ba22 100644
--- a/ruoyi-common/ruoyi-common-core/pom.xml
+++ b/ruoyi-common/ruoyi-common-core/pom.xml
@@ -104,6 +104,13 @@
Use {@link RequestContextListener} or
+ * {@link org.springframework.web.filter.RequestContextFilter} to expose
+ * the current web request. Note that
+ * already exposes the current request by default.
+ *
+ * 修改 spring 上下文存储方式 将 ThreadLocal 替换为 TransmittableThreadLocal
+ * 支持线程上下文切换变量传递 异步获取 spring 上下文
+ *
+ * @author Juergen Hoeller
+ * @author Rod Johnson
+ * @since 2.0
+ * @see RequestContextListener
+ * @see org.springframework.web.filter.RequestContextFilter
+ */
+public abstract class RequestContextHolder {
+
+ private static final boolean jsfPresent =
+ ClassUtils.isPresent("jakarta.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());
+
+ // ThreadLocal 替换为 TransmittableThreadLocal
+ private static final ThreadLocal Exposes the previously bound RequestAttributes instance, if any.
+ * Falls back to the current JSF FacesContext, if any.
+ * @return the RequestAttributes currently bound to the thread
+ * @throws IllegalStateException if no RequestAttributes object
+ * is bound to the current thread
+ * @see #setRequestAttributes
+ * @see ServletRequestAttributes
+ * @see FacesRequestAttributes
+ * @see jakarta.faces.context.FacesContext#getCurrentInstance()
+ */
+ public static RequestAttributes currentRequestAttributes() throws IllegalStateException {
+ RequestAttributes attributes = getRequestAttributes();
+ if (attributes == null) {
+ if (jsfPresent) {
+ attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();
+ }
+ if (attributes == null) {
+ throw new IllegalStateException("No thread-bound request found: " +
+ "Are you referring to request attributes outside of an actual web request, " +
+ "or processing a request outside of the originally receiving thread? " +
+ "If you are actually operating within a web request and still receive this message, " +
+ "your code is probably running outside of DispatcherServlet: " +
+ "In this case, use RequestContextListener or RequestContextFilter to expose the current request.");
+ }
+ }
+ return attributes;
+ }
+
+
+ /**
+ * Inner class to avoid hard-coded JSF dependency.
+ */
+ private static class FacesRequestAttributesFactory {
+
+ @Nullable
+ public static RequestAttributes getFacesRequestAttributes() {
+ try {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ return (facesContext != null ? new FacesRequestAttributes(facesContext) : null);
+ }
+ catch (NoClassDefFoundError err) {
+ // typically for com/sun/faces/util/Util if only the JSF API jar is present
+ return null;
+ }
+ }
+ }
+
+}
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java
index ce3fc04e..6c563a4f 100644
--- a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java
@@ -20,8 +20,6 @@ import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessStatus;
import org.dromara.common.log.event.OperLogEvent;
-import org.dromara.common.satoken.utils.LoginHelper;
-import org.dromara.system.api.model.LoginUser;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.http.HttpMethod;
import org.springframework.validation.BindingResult;
@@ -88,15 +86,6 @@ public class LogAspect {
// *========数据库日志=========*//
OperLogEvent operLog = new OperLogEvent();
- operLog.setTenantId(LoginHelper.getTenantId());
- operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
- // 请求的地址
- String ip = ServletUtils.getClientIP();
- operLog.setOperIp(ip);
- operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
- LoginUser loginUser = LoginHelper.getLoginUser();
- operLog.setOperName(loginUser.getUsername());
- operLog.setDeptName(loginUser.getDeptName());
if (e != null) {
operLog.setStatus(BusinessStatus.FAIL.ordinal());
@@ -106,8 +95,6 @@ public class LogAspect {
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
operLog.setMethod(className + "." + methodName + "()");
- // 设置请求方式
- operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
// 设置消耗时间
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/event/LogEventListener.java b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/event/LogEventListener.java
index 3584e0c6..e0812dd7 100644
--- a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/event/LogEventListener.java
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/event/LogEventListener.java
@@ -11,12 +11,14 @@ import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.utils.ServletUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ip.AddressUtils;
+import org.dromara.common.log.enums.BusinessStatus;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.api.RemoteClientService;
import org.dromara.system.api.RemoteLogService;
import org.dromara.system.api.domain.bo.RemoteLogininforBo;
import org.dromara.system.api.domain.bo.RemoteOperLogBo;
import org.dromara.system.api.domain.vo.RemoteClientVo;
+import org.dromara.system.api.model.LoginUser;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@@ -40,6 +42,18 @@ public class LogEventListener {
@EventListener
public void saveLog(OperLogEvent operLogEvent) {
RemoteOperLogBo sysOperLog = BeanUtil.toBean(operLogEvent, RemoteOperLogBo.class);
+ sysOperLog.setTenantId(LoginHelper.getTenantId());
+ sysOperLog.setStatus(BusinessStatus.SUCCESS.ordinal());
+ // 请求的地址
+ String ip = ServletUtils.getClientIP();
+ sysOperLog.setOperIp(ip);
+ HttpServletRequest request = ServletUtils.getRequest();
+ sysOperLog.setOperUrl(StringUtils.substring(request.getRequestURI(), 0, 255));
+ LoginUser loginUser = LoginHelper.getLoginUser();
+ sysOperLog.setOperName(loginUser.getUsername());
+ sysOperLog.setDeptName(loginUser.getDeptName());
+ // 设置请求方式
+ sysOperLog.setRequestMethod(request.getMethod());
remoteLogService.saveLog(sysOperLog);
}