first commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using JSMachine.WMS.WebHost.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using NPOI.OpenXmlFormats.Dml;
|
||||
|
||||
namespace JSMachine.WMS.WebHost.Filters
|
||||
{
|
||||
public class CustomActionFilter : IActionFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// 方法执行后为结果统一包装
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (context.Exception != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string actionName = ((ControllerActionDescriptor)context.ActionDescriptor).ControllerName;
|
||||
//企望的测试接口需要跳过返回值包装拦截器,不然返回数据又被包装一层,无法解析
|
||||
if (actionName == "Wantit")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ObjectResult objectResult = context.Result as ObjectResult;
|
||||
context.Result = new ObjectResult(new CommonQueryResult<object>(true, true, objectResult?.Value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 方法执行前检测参数是否合法
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
CommonQueryResult<bool> result = new(false, false, false);
|
||||
|
||||
foreach (var item in context.ModelState.Values)
|
||||
{
|
||||
foreach (var error in item.Errors)
|
||||
{
|
||||
result.Msg += error.ErrorMessage + "|";
|
||||
}
|
||||
}
|
||||
result.Msg = result.Msg.TrimEnd('|');
|
||||
context.Result = new JsonResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.WebHost.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace JSMachine.WMS.WebHost.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// 统一捕获控制器执行期间未处理的异常,并转换为标准错误响应。
|
||||
/// </summary>
|
||||
public class ExceptionFilter : IExceptionFilter
|
||||
{
|
||||
/// <summary>
|
||||
/// 记录异常并结束异常传播,向客户端返回内部服务器错误结果。
|
||||
/// </summary>
|
||||
/// <param name="context">包含异常和 HTTP 上下文的过滤器上下文。</param>
|
||||
public void OnException(ExceptionContext context)
|
||||
{
|
||||
CommonQueryResult<bool> commonQueryResult = new(false, false, false) { ErrorCode = ErrorCode.InternalServerError };
|
||||
LogHelper.Error($"发生了全局异常,异常信息 {context.Exception.Message} \n 跟踪信息 {context.Exception.StackTrace}");
|
||||
context.Result = new JsonResult(commonQueryResult);
|
||||
context.ExceptionHandled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using JSMachine.WMS.WebHost.Models;
|
||||
|
||||
namespace JSMachine.WMS.WebHost.Filters
|
||||
{
|
||||
public class RateLimitFilter : IAsyncActionFilter
|
||||
{
|
||||
private IMemoryCache memoryCache;
|
||||
public RateLimitFilter(IMemoryCache memoryCache)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
string remoteIP = context.HttpContext.Connection.RemoteIpAddress?.ToString();
|
||||
string cacheKey = $"LastVisitTick_{remoteIP}";
|
||||
long? lastTick = memoryCache.Get<long?>(cacheKey);
|
||||
if (lastTick == null || Environment.TickCount64 - lastTick > 1000)
|
||||
{
|
||||
memoryCache.Set(cacheKey, Environment.TickCount64, TimeSpan.FromSeconds(10));//距离现在10秒过期
|
||||
return next();
|
||||
}
|
||||
else//1秒之内只允许访问一次
|
||||
{
|
||||
context.Result = new ObjectResult(new CommonQueryResult<object>(true, false, null) { Msg="访问过于频繁"});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user