JwtHandler.cs 3.0 KB

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