Commit 9e3dd12d authored by liudong1993's avatar liudong1993

过滤器增加 控制器方法重复提交验证

parent d36429b6

using System;
namespace Edu.WebApi.Filter
{
/// <summary>
/// 允许重复提交表单属性
/// </summary>
public class AllowRepeatAttribute : Attribute
{
}
}
\ No newline at end of file
...@@ -12,6 +12,7 @@ using Edu.Common.Plugin; ...@@ -12,6 +12,7 @@ using Edu.Common.Plugin;
using Edu.Common.API; using Edu.Common.API;
using Edu.Cache.User; using Edu.Cache.User;
using Edu.WebApi.Helper; using Edu.WebApi.Helper;
using System.Reflection;
namespace Edu.WebApi.Filter namespace Edu.WebApi.Filter
{ {
...@@ -43,31 +44,37 @@ namespace Edu.WebApi.Filter ...@@ -43,31 +44,37 @@ namespace Edu.WebApi.Filter
} }
#region 验证表单重复提交 #region 验证表单重复提交
string controllerName = actionContext.ActionDescriptor.RouteValues["controller"].ToString().ToLower(); var action = actionContext.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
string actionName = actionContext.ActionDescriptor.RouteValues["action"].ToString().ToLower(); string controllerName = action.ControllerName.ToLower();
string actionName = action.ActionName.ToLower();
if (!actionName.ToLower().Contains("get")) if (!actionName.ToLower().Contains("get"))
{ {
string cachedKey = SecurityHelper.MD5(string.Format("cmd={0}&token={1}", controllerName + "/" + actionName, token)); var allowRepeat = action.MethodInfo.GetCustomAttribute(typeof(AllowRepeatAttribute));
try if (allowRepeat == null)
{ {
if (UserReidsCache.Exists(cachedKey))//判断表单是否重复提交 //需进行表单重复提交验证
string cachedKey = SecurityHelper.MD5(string.Format("cmd={0}&token={1}", controllerName + "/" + actionName, token));
try
{ {
actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(new ApiResult if (UserReidsCache.Exists(cachedKey))//判断表单是否重复提交
{ {
Code = (int)ResultCode.FormRepeatSubmit, actionContext.Result = new Microsoft.AspNetCore.Mvc.JsonResult(new ApiResult
Message = "表单重复提交,请稍后再试", {
Data = null Code = (int)ResultCode.FormRepeatSubmit,
}); Message = "表单重复提交,请稍后再试",
Data = null
});
}
else
{
//默认2秒钟之内不能重复提交
UserReidsCache.Set(cachedKey, 1, 2);
}
} }
else catch
{ {
//默认2秒钟之内不能重复提交
UserReidsCache.Set(cachedKey, 1, 2);
} }
} }
catch
{
}
} }
#endregion #endregion
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment