AuthorizationFilter.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Text;
  2. using System.Web;
  3. using Common;
  4. using Extensions;
  5. using Infrastructure;
  6. using Infrastructure.Model;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.Filters;
  9. using Services;
  10. //本命名空间暂时先不改,改动比较大2023年9月2日
  11. namespace Filters
  12. {
  13. /// <summary>
  14. /// </summary>
  15. public class AuthorizationFilter : IAuthorizationFilter
  16. {
  17. private readonly ISysLoginService SysLoginService;
  18. public AuthorizationFilter(ISysLoginService sysLoginService)
  19. {
  20. this.SysLoginService = sysLoginService;
  21. }
  22. /// <summary>
  23. /// </summary>
  24. /// <param name="context"></param>
  25. public void OnAuthorization(AuthorizationFilterContext context)
  26. {
  27. var request = context.HttpContext.Request;
  28. if(!request.Path.Value.ToLower().Contains("noauth/"))
  29. {
  30. string content = "";
  31. if(context.HttpContext.Request.Method.ToLower() == "get")
  32. {
  33. content = context.HttpContext.GetQueryString();
  34. content = GetParam(content, "value");
  35. content = HttpUtility.UrlDecode(content);
  36. content = Decrypt(content);
  37. content = HttpUtility.UrlDecode(content);
  38. if(!string.IsNullOrEmpty(content))
  39. {
  40. Dictionary<string, object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
  41. string queryString = "";
  42. var parameters = context.ActionDescriptor.Parameters;
  43. foreach(var parameter in parameters)
  44. {
  45. string parameterName = parameter.Name;
  46. Type objectType = parameter.ParameterType;
  47. if(objectType.FullName != "System.String")
  48. {
  49. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(objectType);
  50. var entry = assembly.CreateInstance(objectType.FullName);
  51. Type type = entry.GetType();
  52. System.Reflection.PropertyInfo[] propertyInfos = type.GetProperties();
  53. for (int i = 0; i < propertyInfos.Length; i++)
  54. {
  55. foreach (string key in dic.Keys)
  56. {
  57. if (propertyInfos[i].Name == key)
  58. {
  59. object value = dic[key];
  60. string ParameterType = propertyInfos[i].GetMethod.ReturnParameter.ParameterType.Name;
  61. if (ParameterType == "Int32")
  62. {
  63. if(value == null || value == "") value = "0";
  64. value = Convert.ToInt32(value);
  65. }
  66. else if (ParameterType == "Int64[]")
  67. {
  68. value = Tools.SpitLongArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  69. }
  70. else if (ParameterType == "Int32[]")
  71. {
  72. value = Tools.SpitIntArrary(Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"'), ',');
  73. }
  74. else if (ParameterType == "List`1")
  75. {
  76. string val = Newtonsoft.Json.JsonConvert.SerializeObject(value).Replace("[", "").Replace("]", "").Trim('"');
  77. value = Tools.SpitLongArrary(val, ',').ToList();
  78. }
  79. if(value.ToString() == "-1") value = -1;
  80. if(value.ToString() == "[]") value = "";
  81. queryString += key + "=" + value.ToString() + "&";
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. request.QueryString = new QueryString("?" + queryString.TrimEnd('&'));
  89. }
  90. }
  91. else if(context.HttpContext.Request.Method.ToLower() == "delete")
  92. {
  93. string path = request.Path.Value;
  94. string value = path.Substring(path.LastIndexOf("/") + 1);
  95. path = path.Substring(0, path.LastIndexOf("/") + 1);
  96. value = Decrypt(value);
  97. path += value;
  98. request.Path = new PathString(path);
  99. request.RouteValues["id"] = value;
  100. }
  101. else
  102. {
  103. content = context.HttpContext.GetBody();
  104. content = Decrypt(content);
  105. //{"username":"admin","password":"000000"}
  106. request.Body = new MemoryStream(Encoding.UTF8.GetBytes(content));
  107. //验证登录接口
  108. if(request.Path.Value.EndsWith("/oauth2/token"))
  109. {
  110. var scope = request.Query["scope"].ToString();
  111. var grantType = request.Query["grant_type"].ToString();
  112. bool checkLogin = SysLoginService.CheckLogin(scope, grantType, context.HttpContext.GetToken().Replace("Basic ", ""));
  113. if(!checkLogin)
  114. {
  115. string msg = $"请求访问失败,无法访问系统资源";
  116. context.Result = new JsonResult(ApiResult.Error(ResultCode.DENY, msg));
  117. }
  118. }
  119. }
  120. }
  121. }
  122. public string Decrypt(string str)
  123. {
  124. str = str.Trim('"');
  125. str = Encoding.UTF8.GetString(Convert.FromBase64String(str));
  126. return Dbconn.AesDecrypt(str, Base.GlobalConstant.ApiKey, Base.GlobalConstant.ApiIv);
  127. }
  128. public string GetParam(string content, string key)
  129. {
  130. if(content.StartsWith("?")) content = content.Substring(1);
  131. string[] data = content.Split('&');
  132. foreach(string sub in data)
  133. {
  134. if(sub.StartsWith(key + "="))
  135. {
  136. return sub.Substring(sub.IndexOf("=") + 1);
  137. }
  138. }
  139. return "";
  140. }
  141. }
  142. }