集成,总结MES功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
2.2 KiB

  1. using Furion;
  2. using Furion.Authorization;
  3. using Furion.DataEncryption;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. using System.Threading.Tasks;
  7. namespace BPA.MES.Base.Web.Core;
  8. public class JwtHandler : AppAuthorizeHandler
  9. {
  10. /// <summary>
  11. /// 重写 Handler 添加自动刷新收取逻辑
  12. /// </summary>
  13. /// <param name="context"></param>
  14. /// <returns></returns>
  15. public override async Task HandleAsync(AuthorizationHandlerContext context)
  16. {
  17. var IsClient = App.HttpContext.Request.Headers["IsClient"].ToString();
  18. if (IsClient == "true")
  19. {
  20. //获取所有未成功验证的需求
  21. var pendingRequirements = context.PendingRequirements;
  22. // 通过授权验证
  23. foreach (var requirement in pendingRequirements)
  24. {
  25. context.Succeed(requirement);
  26. }
  27. }
  28. else
  29. {
  30. // 自动刷新 token
  31. if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext(), expiredTime: 43200, refreshTokenExpiredTime: 43225))
  32. {
  33. await AuthorizeHandleAsync(context);
  34. }
  35. else
  36. {
  37. context.Fail(); // 授权失败
  38. }
  39. }
  40. }
  41. public override Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
  42. {
  43. // 这里写您的授权判断逻辑,授权通过返回 true,否则返回 false
  44. return Task.FromResult(CheckAuthorzie(httpContext));
  45. }
  46. /// <summary>
  47. /// 检查权限
  48. /// </summary>
  49. /// <param name="httpContext"></param>
  50. /// <returns></returns>
  51. private static bool CheckAuthorzie(DefaultHttpContext httpContext)
  52. {
  53. // 获取权限特性
  54. var IsClient = App.HttpContext.Request.Headers["IsClient"].ToString();
  55. if (IsClient == "true")
  56. {
  57. return true;
  58. }
  59. var securityDefineAttribute = httpContext.GetMetadata<SecurityDefineAttribute>();
  60. if (securityDefineAttribute == null)
  61. {
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69. }