CrazyIter 4 år sedan
förälder
incheckning
ddcd8f153a

+ 46 - 0
HiTeachCE/Context/ApiExceptionFilterAttribute.cs

@@ -0,0 +1,46 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace HiTeachCE.Context
+{
+    public class ApiExceptionFilterAttribute : ExceptionFilterAttribute
+    {
+        public override void OnException(ExceptionContext context)
+        {
+            context.Result = BuildExceptionResult(context.Exception);
+            base.OnException(context);
+        }// <summary>
+        /// 包装处理异常格式
+        /// </summary>
+        /// <param name="ex"></param>
+        /// <returns></returns>
+        private JsonResult BuildExceptionResult(Exception ex)
+        {
+            int code = 0;
+            string message = "";
+            string innerMessage = "";
+            //应用程序业务级异常
+            if (ex is ApplicationException)
+            {
+                code = 501;
+                message = ex.Message;
+            }
+            else
+            {
+                // exception 系统级别异常,不直接明文显示的
+                code = 500;
+                message = "发生系统级别异常";
+                innerMessage = ex.Message;
+            }
+
+            if (ex.InnerException != null && ex.Message != ex.InnerException.Message)
+                innerMessage += "," + ex.InnerException.Message;
+
+            return new JsonResult(new { code, message, innerMessage });
+        }
+    }
+}

+ 3 - 0
HiTeachCE/Extension/Jwt/BlackListJwtSecurityTokenHandler.cs

@@ -22,6 +22,9 @@ namespace HiTeachCE.Extension
         public override ClaimsPrincipal ValidateToken(string token, TokenValidationParameters validationParameters,
             out SecurityToken validatedToken)
         {
+            if (string.IsNullOrEmpty(token)) {
+                //return claimsPrincipal;
+            }
             var claimsPrincipal = base.ValidateToken(token, validationParameters, out validatedToken);
 
             //解析ClaimsPrincipal取出UserId、Iat和Jti

+ 1 - 1
HiTeachCE/Extension/Jwt/JwtAuth.cs

@@ -52,7 +52,7 @@ namespace HiTeachCE.Extension
             {
                 ///https://blog.csdn.net/sinat_14899485/article/details/88591848 jwt 黑名单
                 //o.SecurityTokenValidators.Clear();
-                o.SecurityTokenValidators.Add(new BlackListJwtSecurityTokenHandler()); /// 自定义黑名单拦截
+              //  o.SecurityTokenValidators.Add(new BlackListJwtSecurityTokenHandler()); /// 自定义黑名单拦截
                 o.TokenValidationParameters = tokenValidationParameters;
                 o.Events = new JwtBearerEvents
                 {

+ 9 - 1
HiTeachCE/Startup.cs

@@ -31,6 +31,7 @@ using static HiTeachCE.Controllers.LoginController;
 using VueCliMiddleware;
 using Microsoft.AspNetCore.SpaServices;
 using HiTeachCE.Extension.Mqtt;
+using HiTeachCE.Context;
 
 namespace HiTeachCE
 {
@@ -90,6 +91,11 @@ namespace HiTeachCE
                 x.MultipartHeadersLengthLimit = int.MaxValue;
             });
             services.MQTTConnection();
+            // Add framework services.
+            services.AddMvc(options =>
+            {
+                options.Filters.Add(new ApiExceptionFilterAttribute());
+            });
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -105,7 +111,9 @@ namespace HiTeachCE
                 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                 app.UseHsts();
             }
-            app.UseMiddleware<HiTeachCE.Context.HttpGlobalExceptionInvoke>();
+
+            
+          //  app.UseMiddleware<HiTeachCE.Context.HttpGlobalExceptionInvoke>();
             app.UseHttpsRedirection();
             app.UseStaticFiles();