AopActionFilter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | using System.Diagnostics; using System.Text; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; using Newtonsoft.Json; using RuoVea.ExUtil; using JsonSerializer = System.Text.Json.JsonSerializer; namespace RuoVea.ApiService.Filters; /// <summary> /// 方法过滤器 /// </summary> public class AopActionFilter : IAsyncActionFilter { private static readonly List< string > IgnoreApi = new () { "api/sysfile/" , "api/captcha" , "/chathub" }; private static readonly List< string > IgnorePowerApi = new () { "api/sysfile/" , "api/captcha" , "/chathub" , "login" }; private readonly SysLogService _logService; private readonly OperatorService _operatorService; public AopActionFilter(SysLogService logService, OperatorService operatorService) { _logService = logService; _operatorService = operatorService; } private static bool IsIgnoreApi( string url) { var isIgnore = false ; foreach ( var item in IgnorePowerApi.Where(url.Contains)) { isIgnore = true ; } return isIgnore; } public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var user = _operatorService.User; #region 判断授权Api资源 var superRole = AppUtils.Configuration[KeyUtils.SUPERROLEID]; var urls = context.HttpContext.Request.Path.ToString().ToLower(); if (!user.RoleArray.Contains( long .Parse(superRole)) && context.HttpContext.Request.Method != "GET" && context.HttpContext.Request.Method != "OPTIONS" && !IsIgnoreApi(urls)) { Console.WriteLine( "=======判断权限========" ); var redisStr = RedisService.cli.Get(KeyUtils.AUTHORIZZATIONAPI + ":" + user.Id); var apiList = ! string .IsNullOrEmpty(redisStr) ? JsonSerializer.Deserialize<List<SysMenuApiUrl>>(redisStr) : null ; if (apiList != null && !apiList.Exists(api => api.method == context.HttpContext.Request.Method && urls.Contains(api.url.ToLower()))) { context.Result = new JsonResult(JResult< int >.Error( "您无权限访问当前资源" )); return ; } } #endregion #region 安全签名认证 var request = context.HttpContext.Request; var urlPath = request.Path.ToString().ToLower(); var isSecurity = true ; foreach ( var item in IgnoreApi.Where(item => urlPath.Contains(item))) { isSecurity = true ; } if (!isSecurity) { var method = request.Method; string appkey = string .Empty, timestamp = string .Empty, signature = string .Empty; //客户的唯一标示 if (request.Headers.ContainsKey( "appkey" )) { appkey = request.Headers[ "appkey" ].ToString(); //Console.WriteLine("appkey:"+appkey); } //13位时间戳 if (request.Headers.ContainsKey( "timestamp" )) { timestamp = request.Headers[ "timestamp" ]; //Console.WriteLine("timestamp:"+timestamp); } //签名 if (request.Headers.ContainsKey( "signature" )) { signature = request.Headers[ "signature" ]; //Console.WriteLine("signature:"+signature); } if ( string .IsNullOrEmpty(appkey) || string .IsNullOrEmpty(timestamp) || string .IsNullOrEmpty(signature)) { Logger.Info( "ApiSecurity——请求不合法" ); context.Result = new JsonResult(JResult< int >.Error( "请求不合法" )); return ; } var security = AppUtils.GetConfig(Security.Name).Get<Security>(); if (appkey != security.AppKey) { Logger.Info( "ApiSecurity——请求不合法-k" ); context.Result = new JsonResult(JResult< int >.Error( "请求不合法-k" )); return ; } //判断timespan是否有效 double ts1 = 0; double ts2 = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds; bool timespanvalidate = double .TryParse(timestamp, out ts1); double ts = ts2 - ts1; bool falg = ts > 1200 * 1000; //1分钟有效 if (falg || (!timespanvalidate)) { Logger.Info( "ApiSecurity——请求不合法-t" ); context.Result = new JsonResult(JResult< int >.Error( "请求不合法-t" )); return ; } //根据请求类型拼接参数 IDictionary< string , string > parameters = new Dictionary< string , string >(); string data = string .Empty; switch (method) { case "POST" : context.HttpContext.Request.Body.Position = 0; StreamReader stream = new StreamReader(context.HttpContext.Request.Body); string body = await stream.ReadToEndAsync(); //Console.WriteLine("body:"+ body); data = body; context.HttpContext.Request.Body.Seek(0, SeekOrigin.Begin); break ; case "PUT" : context.HttpContext.Request.Body.Position = 0; StreamReader streamPut = new StreamReader(context.HttpContext.Request.Body); string bodyPut = await streamPut.ReadToEndAsync(); //Console.WriteLine("put:"+ bodyPut); data = bodyPut; context.HttpContext.Request.Body.Seek(0, SeekOrigin.Begin); break ; case "DELETE" : context.HttpContext.Request.Body.Position = 0; StreamReader streamDel = new StreamReader(context.HttpContext.Request.Body); string bodyDel = await streamDel.ReadToEndAsync(); //Console.WriteLine("put:"+ bodyPut); data = bodyDel; context.HttpContext.Request.Body.Seek(0, SeekOrigin.Begin); break ; case "GET" : { var query = request.Query; foreach ( var item in query) { parameters.Add(item.Key, item.Value); } // 第二步:把字典按Key的字母顺序排序 IDictionary< string , string > sortedParams = new SortedDictionary< string , string >(parameters); using IEnumerator<KeyValuePair< string , string >> dem = sortedParams.GetEnumerator(); // 第三步:把所有参数名和参数值串在一起 StringBuilder stringBuilder = new StringBuilder(); while (dem.MoveNext()) { string key = dem.Current.Key; string value = dem.Current.Value; if (! string .IsNullOrEmpty(key)) { stringBuilder.Append(key).Append(value); } } data = stringBuilder.ToString(); //Console.WriteLine("GET:"+JsonConvert.SerializeObject(data)); break ; } } if (!ApiSecurityValidate(timestamp, appkey, data, signature)) { Logger.Info( "ApiSecurity——参数不合法-Sign" ); context.Result = new JsonResult(JResult< int >.Error( "参数不合法" )); return ; } //Console.WriteLine("success"); } #endregion //验证实体 if (!context.ModelState.IsValid) { context.Result = new JsonResult(JResult< string >.Error( "参数不能为空~" )); return ; } //开始计时 var stopwatch = Stopwatch.StartNew(); var actionResult = await next(); stopwatch.Stop(); //读取返回类型以及数据 var (isObject, actionData, logResult) = CheckResult(actionResult.Result); #region 收集日志信息 if (!SkipLogging(context)) { //接口Type var type = (context.ActionDescriptor as ControllerActionDescriptor)?.ControllerTypeInfo.AsType(); var arguments = context.ActionArguments; var parametersStr = string .Empty; if (arguments.Count > 0) { parametersStr = JsonSerializer.Serialize(arguments); } //构建实体 var logInfo = new SysLogDto() { Level = LogEnum.Info, LogType = LogTypeEnum.Operate, Module = type?.FullName, Method = context.HttpContext.Request.Method, OperateUser = user.Username, Parameters = parametersStr, IP = CommonUtils.GetIp(), Address = context.HttpContext.Request.Path + context.HttpContext.Request.QueryString, Browser = CommonUtils.GetBrowser(), }; logInfo.ExecutionDuration = Convert.ToInt32(stopwatch.Elapsed.TotalMilliseconds); if (! string .IsNullOrEmpty(logResult)) { logInfo.ReturnValue = logResult.Replace( "\\" , "" ).CutString(1000); } //保存日志信息 await _logService.AddAsync(logInfo); } #endregion //返回统一格式 if (isObject && !SkipJsonResult(context)) { actionResult.Result = new JsonResult(JResult< object >.Success(actionData)); } Console.WriteLine( "Aop-Success" ); } /// <summary> /// 判断类和方法头上的特性是否要进行Action拦截 /// </summary> /// <param name="actionContext"></param> /// <returns></returns> private static bool SkipLogging(ActionContext actionContext) { return actionContext.ActionDescriptor.EndpointMetadata.Any(m => m.GetType().FullName == typeof (NoAuditLogAttribute).FullName); } /// <summary> /// 判断类和方法头上的特性是否要进行非统一结果返回拦截 /// </summary> /// <param name="actionContext"></param> /// <returns></returns> private static bool SkipJsonResult(ActionContext actionContext) { return actionContext.ActionDescriptor.EndpointMetadata.Any(m => m.GetType().FullName == typeof (NoJsonResultAttribute).FullName); } /// <summary> /// 验证参数是否正确 /// </summary> /// <param name="timeStamp"></param> /// <param name="appId"></param> /// <param name="data"></param> /// <param name="signature"></param> /// <returns></returns> private static bool ApiSecurityValidate( string timeStamp, string appId, string data, string signature) { var security = AppUtils.GetConfig(Security.Name).Get<Security>(); //签名key var signKey = security.SignKey; //拼接签名数据 var signStr = appId + signKey + timeStamp + data; var newSign = signStr.MDString(); return newSign == signature; } /// <summary> /// 验证返回类型是否满足Object格式 /// </summary> /// <param name="result"></param> /// <returns></returns> private static ( bool , object ?, string ?) CheckResult(IActionResult? result) { return result switch { ObjectResult objectResult => ( true , objectResult.Value, JsonSerializer.Serialize(objectResult.Value)), JsonResult jsonResult => ( true , jsonResult.Value, JsonSerializer.Serialize(jsonResult.Value)), ContentResult contentResult => ( false , contentResult.Content, JsonSerializer.Serialize(contentResult.Content)), _ => ( true , null , null )! }; } } |
作者:staluo
出处:https://www.cnblogs.com/stry/p/17191647.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构