Blame view

netcore/src/Application/NCC.API.Core/Handlers/JwtHandler.cs 2.45 KB
de2bd2f9   “wangming”   项目初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  using NCC.Authorization;
  using NCC.Common.Core.Manager;
  using NCC.DataEncryption;
  using Microsoft.AspNetCore.Authorization;
  using Microsoft.AspNetCore.Http;
  using System.Collections.Generic;
  using System.Threading.Tasks;
  
  namespace NCC.API.Core
  {
      /// <summary>
      /// jwt处理程序
      /// </summary>
      public class JwtHandler : AppAuthorizeHandler
      {
          /// <summary>
          /// 重写 Handler 添加自动刷新
          /// </summary>
          /// <param name="context"></param>
          /// <returns></returns>
          public override async Task HandleAsync(AuthorizationHandlerContext context)
          {
              // 自动刷新Token
              if (JWTEncryption.AutoRefreshToken(context, context.GetCurrentHttpContext()))
              {
                  await AuthorizeHandleAsync(context);
              }
              else context.Fail(); // 授权失败
          }
  
          /// <summary>
          /// 授权判断逻辑,授权通过返回 true,否则返回 false
          /// </summary>
          /// <param name="context"></param>
          /// <param name="httpContext"></param>
          /// <returns></returns>
          public override async Task<bool> PipelineAsync(AuthorizationHandlerContext context, DefaultHttpContext httpContext)
          {
              // 此处已经自动验证 Jwt Token的有效性了,无需手动验证
              return await CheckAuthorzieAsync(httpContext);
          }
  
          /// <summary>
          /// 检查权限
          /// </summary>
          /// <param name="httpContext"></param>
          /// <returns></returns>
          private static async Task<bool> CheckAuthorzieAsync(DefaultHttpContext httpContext)
          {
              // 管理员跳过判断
              var userManager = App.GetService<IUserManager>();
              if (userManager.IsAdministrator) return true;
  
              // 路由名称
              var routeName = httpContext.Request.Path.Value.Substring(1).Replace("/", ":");
  
              // 默认路由(获取登录用户信息)
              var defalutRoute = new List<string>()
              {
                  "api:oauth:CurrentUser"
              };
  
              if (defalutRoute.Contains(routeName)) return true;
  
              // 获取用户权限集合(按钮或API接口)
              //var permissionList = await App.GetService<ISysMenuService>().GetLoginPermissionList(userManager.UserId);
  
              // 检查授权
              //return permissionList.Contains(routeName);
              return true;
          }
      }
  }