From d43a3e4971792f043cfbe0d32a7d5d0f0e841be8 Mon Sep 17 00:00:00 2001
From: TanYibin <5491541@qq.com>
Date: Tue, 4 Apr 2023 11:08:00 +0800
Subject: [PATCH] 修正定时任务获取操作人错误

---
 huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java                 | 11 ++++++++++-
 huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/TaskBuildAudienceAspect.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/TaskBuildAudienceAspect.java

diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java
index 651a396..c1be785 100644
--- a/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java
+++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/HuahengJwtUtil.java
@@ -43,6 +43,9 @@ public class HuahengJwtUtil {
 
     /** 全仓CODE */
     public static final String ALL_WAREHOUSE_CODE = "ALL_WAREHOUSE";
+    
+    /** 定时任务操作人 */
+    public static final String TASK_AUDIENCE_NAME = "SYSTEM_TASK";
 
     public static final String SYSTEM_ACTIVATION_CODE_FILE_NAME = "ActivationCode.txt";
 
@@ -230,7 +233,12 @@ public class HuahengJwtUtil {
      * @return
      */
     public static String getCurrentOperator() {
-        LoginUser loginUser = SecurityUtils.getSubject().getPrincipal() != null ? (LoginUser)SecurityUtils.getSubject().getPrincipal() : null;
+        LoginUser loginUser = null;
+        try {
+            loginUser = SecurityUtils.getSubject().getPrincipal() != null ? (LoginUser)SecurityUtils.getSubject().getPrincipal() : null;
+        } catch (Exception e) {
+            loginUser = null;
+        }
         if (loginUser != null) {
             return loginUser.getRealname();
         }
@@ -253,3 +261,4 @@ public class HuahengJwtUtil {
         }
     }
 }
+
diff --git a/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/TaskBuildAudienceAspect.java b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/TaskBuildAudienceAspect.java
new file mode 100644
index 0000000..3a294b6
--- /dev/null
+++ b/huaheng-wms-core/src/main/java/org/jeecg/utils/aspect/TaskBuildAudienceAspect.java
@@ -0,0 +1,57 @@
+package org.jeecg.utils.aspect;
+
+import java.lang.reflect.Method;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.jeecg.utils.HuahengJwtUtil;
+import org.jeecg.utils.config.ApplicationConfig;
+import org.jeecg.utils.support.ApiAuthentication;
+import org.jeecg.utils.support.PassApiAuthentication;
+import org.jeecg.utils.support.RSA256Key;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import com.auth0.jwt.JWT;
+import com.auth0.jwt.JWTVerifier;
+import com.auth0.jwt.algorithms.Algorithm;
+import com.auth0.jwt.exceptions.JWTVerificationException;
+import com.auth0.jwt.interfaces.DecodedJWT;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 构建定时任务操作人拦截器
+ * @author     TanYibin
+ * @createDate 2023年4月4日
+ */
+@Slf4j
+@Aspect
+@Component
+@EnableAsync
+public class TaskBuildAudienceAspect {
+
+    @Pointcut("execution(* org.jeecg.modules.wms.monitor.job.*Task.execute(..))")
+    public void executeTask() {}
+
+    /**
+     * API Token 验证
+     * @author                           TanYibin
+     * @createDate                       2023年2月14日
+     * @param      joinPoint
+     * @throws     NoSuchMethodException
+     * @throws     Throwable
+     */
+    @Before("executeTask()")
+    public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException, Throwable {
+        new ApiAuthentication.ApiAuthenticationBuild().audience(HuahengJwtUtil.TASK_AUDIENCE_NAME).bulid();
+    }
+}
--
libgit2 0.22.2