基础服务api
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.
 
 

79 lines
2.9 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.Security.Claims;
  7. using System.Threading.Tasks;
  8. namespace BPA.SAAS.Manage.Web.Core
  9. {
  10. public class JwtHandler : AppAuthorizeHandler
  11. {
  12. /// <summary>
  13. /// 重写 Handler 添加自动刷新
  14. /// </summary>
  15. /// <param name="context"></param>
  16. /// <returns></returns>
  17. public override async Task HandleAsync(AuthorizationHandlerContext context)
  18. {
  19. // 自动刷新Token
  20. if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext(),
  21. App.GetOptions<JWTSettingsOptions>().ExpiredTime))
  22. {
  23. await AuthorizeHandleAsync(context);
  24. }
  25. else
  26. {
  27. context.Fail(); // 授权失败
  28. DefaultHttpContext currentHttpContext = context.GetCurrentHttpContext();
  29. if (currentHttpContext == null)
  30. return;
  31. currentHttpContext.SignoutToSwagger();
  32. }
  33. }
  34. /// <summary>
  35. /// 授权判断逻辑,授权通过返回 true,否则返回 false
  36. /// </summary>
  37. /// <param name="context"></param>
  38. /// <param name="httpContext"></param>
  39. /// <returns></returns>
  40. public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
  41. {
  42. // 此处已经自动验证 Jwt Token的有效性了,无需手动验证
  43. return await CheckAuthorzieAsync(httpContext);
  44. }
  45. /// <summary>
  46. /// 检查权限
  47. /// </summary>
  48. /// <param name="httpContext"></param>
  49. /// <returns></returns>
  50. private static async Task<bool> CheckAuthorzieAsync(DefaultHttpContext httpContext)
  51. {
  52. // 获取权限特性 FromHeader教研数据
  53. //var securityDefineAttribute = httpContext.GetMetadata<SecurityDefineAttribute>();
  54. //if (securityDefineAttribute == null) return true;
  55. var userId = App.User?.FindFirstValue("UserId");
  56. // 解析服务
  57. // var userManager = httpContext.RequestServices.GetService<IUserService>();
  58. // var loginType = App.User?.FindFirstValue("LoginType");
  59. //登录认证服务
  60. return true;
  61. //if ("0".Equals(loginType)) {
  62. // var securities = await userManager.CheckUser(userId);
  63. // // 检查授权
  64. // return securities;
  65. //}
  66. //if ("1".Equals(loginType)) {
  67. // var securities = await userManager.CheckUser(userId);
  68. // // 检查授权
  69. // return securities;
  70. //}
  71. //return false;
  72. }
  73. }
  74. }