From a7b6a32774c3d39b3c2dc9728b6ca0300842dbf1 Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期四, 02 四月 2026 09:50:57 +0800
Subject: [PATCH] 修复JWT 鉴权时 无自定义标签 时报空指针异常的问题

---
 WebAPI/Service/JwtAuthorizeAttribute.cs |   23 +++++++++--------------
 1 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/WebAPI/Service/JwtAuthorizeAttribute.cs b/WebAPI/Service/JwtAuthorizeAttribute.cs
index 382f7eb..f5f90bc 100644
--- a/WebAPI/Service/JwtAuthorizeAttribute.cs
+++ b/WebAPI/Service/JwtAuthorizeAttribute.cs
@@ -33,7 +33,7 @@
     {
         try
         {
-            // 妫�鏌ュ尶鍚嶈闂�
+            // 妫�鏌ュ尶鍚嶈闂�(璁よ瘉)
             if (IsAnonymousAllowed(actionContext)) return;
 
             // 鑾峰彇Token
@@ -58,25 +58,20 @@
 
             // 楠岃瘉鏄惁鎷ユ湁璁块棶妯″潡鐨勬潈闄�
             // 鏈変袱绉嶅垽鏂柟寮� 涓�绉嶆槸閫氳繃瑙掕壊鍘诲垽鏂紝涓�绉嶆槸閫氳繃鐢ㄦ埛鍘诲垽鏂� 鐩墠鏆傛椂涓嶅仛鍖哄垎锛屽悗缁彲澧炲姞绯荤粺鍙傛暟鏉ヨ繘琛屽尯鍒�
-            // 鑾峰彇鎺у埗鍣ㄦ弿杩板櫒
-            HttpControllerDescriptor controllerDescriptor = actionContext.ControllerContext.ControllerDescriptor;
-            // //鑾峰彇鎺у埗鍣ㄧ被鍨�
-            Type controllerType = controllerDescriptor.ControllerType;
-            // // 鑾峰彇鎺у埗鍣ㄧ骇鍒爣绛�
-            var controllerAttr = controllerType.GetCustomAttribute<PermissionAttribute>();
-            // 鑾峰彇鍔ㄤ綔绾у埆鏍囩
-            var actionAttr = actionContext.ActionDescriptor
-            .GetCustomAttributes<PermissionAttribute>()
-            .FirstOrDefault();
+            // 瀹夊叏鎺у埗鍣ㄦ弿鏍囩
+            var controllerAttr = actionContext.ControllerContext?.ControllerDescriptor?.GetCustomAttributes<PermissionAttribute>().FirstOrDefault();
+
+            // 瀹夊叏鑾峰彇Action鏍囩
+            var actionAttr = actionContext.ActionDescriptor?.GetCustomAttributes<PermissionAttribute>().FirstOrDefault();
 
             string PermissionStr = string.Empty;
             // 鍒ゆ柇鎺у埗鍣ㄧ骇鍒笂鏄惁鍚敤浜嗛壌鏉冩爣绛�
-            if(controllerAttr != null && !string.IsNullOrWhiteSpace(controllerAttr.HModName))
+            if(controllerAttr != null && !string.IsNullOrWhiteSpace(controllerAttr?.HModName))
             {
                 PermissionStr += controllerAttr.HModName;
 
                 // 鏌ョ湅鏄惁闇�瑕佹洿缁嗙矑搴︾殑鎺у埗
-                if(actionAttr != null && !string.IsNullOrWhiteSpace(actionAttr.Operate))
+                if(actionAttr != null && !string.IsNullOrWhiteSpace(actionAttr?.Operate))
                 {
                     // 璇ュ姩浣滃搴旂殑妯″潡鍚嶅拰鎺у埗鍣ㄥ悕鏄惁涓嶄竴鑷�
                     if (string.IsNullOrWhiteSpace(actionAttr.HModName))
@@ -99,7 +94,7 @@
                     }
                 }
                 
-            }else if (string.IsNullOrWhiteSpace(actionAttr.HModName) && string.IsNullOrWhiteSpace(actionAttr.Operate))
+            }else if (!string.IsNullOrWhiteSpace(actionAttr?.HModName) && !string.IsNullOrWhiteSpace(actionAttr?.Operate))
             {
                 // 鍗曠嫭鍦ㄥ姩浣滀笂鍚敤閴存潈鍔熻兘
                 PermissionStr = actionAttr.HModName + actionAttr.Operate;

--
Gitblit v1.9.1