JwtHandler.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Furion;
  2. using Furion.Authorization;
  3. using Furion.DataEncryption;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using YBEE.EQM.Application;
  10. using YBEE.EQM.Core;
  11. namespace YBEE.EQM.Web.Core;
  12. public class JwtHandler : AppAuthorizeHandler
  13. {
  14. /// <summary>
  15. /// 重写 Handler 添加自动刷新
  16. /// </summary>
  17. /// <param name="context"></param>
  18. /// <param name="httpContext"></param>
  19. /// <returns></returns>
  20. public override async Task HandleAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
  21. {
  22. // 自动刷新Token
  23. if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext(), App.GetOptions<JWTSettingsOptions>().ExpiredTime, App.GetOptions<RefreshTokenSettingOptions>().ExpiredTime))
  24. {
  25. await AuthorizeHandleAsync(context);
  26. }
  27. else
  28. {
  29. context.Fail(); // 授权失败
  30. DefaultHttpContext currentHttpContext = context.GetCurrentHttpContext();
  31. if (currentHttpContext == null)
  32. {
  33. return;
  34. }
  35. currentHttpContext.SignoutToSwagger();
  36. }
  37. }
  38. /// <summary>
  39. /// 授权判断逻辑,授权通过返回 true,否则返回 false
  40. /// </summary>
  41. /// <param name="context"></param>
  42. /// <param name="httpContext"></param>
  43. /// <returns></returns>
  44. public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
  45. {
  46. // 此处已经自动验证 Jwt Token的有效性了,无需手动验证
  47. return await CheckAuthorzieAsync(httpContext);
  48. }
  49. /// <summary>
  50. /// 检查权限
  51. /// </summary>
  52. /// <param name="httpContext"></param>
  53. /// <returns></returns>
  54. private static async Task<bool> CheckAuthorzieAsync(DefaultHttpContext httpContext)
  55. {
  56. // 管理员跳过判断
  57. //var isSuperAdmin = bool.Parse()
  58. if (bool.TryParse(App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value ?? "False", out bool isSuperAdmin))
  59. {
  60. if (isSuperAdmin)
  61. {
  62. return true;
  63. }
  64. }
  65. // 路由名称
  66. var routeName = httpContext.Request.Path.Value[1..].Replace("/", ":");
  67. // 默认路由(获取登录用户信息)
  68. var defalutRoute = new List<string>()
  69. {
  70. "get-login-user", //登录
  71. };
  72. if (defalutRoute.Contains(routeName))
  73. {
  74. return true;
  75. }
  76. // 获取用户权限集合(按钮或API接口)
  77. var allPermissionList = await App.GetService<ISysMenuService>().GetAllPermissionList();
  78. var currUserId = int.Parse(App.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value ?? "0");
  79. var permissionList = await App.GetService<ISysMenuService>().GetLoginPermissionList(currUserId);
  80. // 检查授权
  81. // 菜单中没有配置按钮权限,则不限制
  82. return allPermissionList.All(u => u != routeName) || permissionList.Contains(routeName);
  83. }
  84. }