浏览代码

添加拦截器,新接口走新的服务

lcl 1 年之前
父节点
当前提交
0dec62fdfd
共有 7 个文件被更改,包括 134 次插入0 次删除
  1. 25 0
      Filter/AuthorizeFilter.cs
  2. 50 0
      Filter/GlobalActionFilter.cs
  3. 16 0
      Filter/GlobalExceptionsFilter.cs
  4. 19 0
      Filter/ResourceFilter.cs
  5. 19 0
      Filter/ResultFilter.cs
  6. 1 0
      MySystem.csproj
  7. 4 0
      Startup.cs

+ 25 - 0
Filter/AuthorizeFilter.cs

@@ -0,0 +1,25 @@
+using System;
+using Library;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace MySystem
+{
+    public class AuthorizeFilter : IAuthorizationFilter
+    {
+        /// <summary>
+        /// 请求验证,当前验证部分不要抛出异常,ExceptionFilter不会处理
+        /// </summary>
+        /// <param name="context"></param>
+        public void OnAuthorization(AuthorizationFilterContext context)
+        {
+        //这里可以做复杂的权限控制操作
+        //if (context.HttpContext.User.Identity.Name != "1") //简单的做一个示范
+        //{
+        // //未通过验证则跳转到无权限提示页
+        // RedirectToActionResult content = new RedirectToActionResult("NoAuth", "Exception", null);
+        // context.Result = content;
+        //
+
+        }
+    }
+}

+ 50 - 0
Filter/GlobalActionFilter.cs

@@ -0,0 +1,50 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Library;
+using LitJson;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.Filters;
+using MySystem.Models;
+
+namespace MySystem
+{
+    public class GlobalActionFilter : IActionFilter
+    {
+        public void OnActionExecuted(ActionExecutedContext context)
+        {
+            //执行方法后执行这
+        }
+
+        public void OnActionExecuting(ActionExecutingContext context)
+        {
+            //执行方法前先执行这
+            var stringArgs = context.ActionArguments.ToList();
+            foreach (var keyValue in stringArgs)
+            {
+                string path = context.ActionDescriptor.AttributeRouteInfo.Template.ToLower();
+                if(keyValue.Key == "value" && AllowApiList().Contains(path))
+                {
+                    string result = function.PostWebRequest("https://test.mpap2.kexiaoshuang.com/" + path, "value=" + keyValue.Value.ToString());
+                    result = result.Replace("null", "\"\"");
+                    result = result.Replace("\"status\"", "\"Status\"");
+                    result = result.Replace("\"data\"", "\"Data\"");
+                    result = result.Replace("\"other\"", "\"Other\"");
+                    result = result.Replace("\"timestamp\"", "\"Timestamp\"");
+                    result = result.Replace("\"info\"", "\"Info\"");
+                    JsonResult obj = new JsonResult(Newtonsoft.Json.JsonConvert.DeserializeObject<AppResultJson>(result));
+                    context.Result = obj;
+                    return;
+                }
+            }
+        }
+
+        private List<string> AllowApiList()
+        {
+            List<string> result = new List<string>();
+            result.Add("api/v1/merchantaddinfo/add");
+            result.Add("api/v1/merchantaddinfo/businessscope");
+            return result;
+        }
+    }
+}

+ 16 - 0
Filter/GlobalExceptionsFilter.cs

@@ -0,0 +1,16 @@
+using System;
+using Library;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace MySystem
+{
+    public class GlobalExceptionsFilter : IExceptionFilter
+    {
+        public void OnException(ExceptionContext context)
+        {
+            string Message = context.Exception.Message;
+            string StackTrace = context.Exception.StackTrace;
+            function.WriteLog(DateTime.Now.ToString() + "\r\n" + Message + "\r\n" + StackTrace, "Global全局异常处理日志");
+        }
+    }
+}

+ 19 - 0
Filter/ResourceFilter.cs

@@ -0,0 +1,19 @@
+using System;
+using Library;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace MySystem
+{
+    public class ResourceFilter : Attribute,IResourceFilter
+    {
+        public void OnResourceExecuted(ResourceExecutedContext context)
+        {
+            // 执行完后的操作
+        }
+
+        public void OnResourceExecuting(ResourceExecutingContext context)
+        {
+            // 执行中的过滤器管道
+        }
+    }
+}

+ 19 - 0
Filter/ResultFilter.cs

@@ -0,0 +1,19 @@
+using System;
+using Library;
+using Microsoft.AspNetCore.Mvc.Filters;
+
+namespace MySystem
+{
+    public class ResultFilter : Attribute, IResultFilter
+    {
+        public void OnResultExecuted(ResultExecutedContext context)
+        { 
+            // 在结果执行之后调用的操作...
+        }
+
+        public void OnResultExecuting(ResultExecutingContext context)
+        {
+            // 在结果执行之前调用的一系列操作
+        }
+    }
+}

+ 1 - 0
MySystem.csproj

@@ -69,6 +69,7 @@
     <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
     <PackageReference Include="MySql.Data" Version="8.0.18" />
     <PackageReference Include="CSRedisCore" Version="3.6.5" />
+    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0"/>
   </ItemGroup>
   <ItemGroup>
     <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />

+ 4 - 0
Startup.cs

@@ -41,6 +41,7 @@ namespace MySystem
             // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));//是否允许跨域
             // 测试完成后必须注释掉
             services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
+            services.AddMvcCore().AddNewtonsoftJson();
             services.AddMvc(options =>
             {
                 options.EnableEndpointRouting = false;
@@ -56,6 +57,9 @@ namespace MySystem
             {
                 x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
             });
+            services.AddControllers(option => {
+                option.Filters.Add(new GlobalActionFilter());
+            });
             //生成密钥
             var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
             var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);