【NET CORE微服务一条龙应用】第三章 认证授权与动态权限配置

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 【NET CORE微服务一条龙应用】第三章 认证授权与动态权限配置 介绍 系列目录:【NET CORE微服务一条龙应用】开始篇与目录 在微服务的应用中,统一的认证授权是必不可少的组件,本文将介绍微服务中网关和子服务如何使用统一的权限认证 主要介绍内容为: 1、子服务如何实现和网关相同的鉴权方式 2.

【NET CORE微服务一条龙应用】第三章 认证授权与动态权限配置

介绍

系列目录:【NET CORE微服务一条龙应用】开始篇与目录

在微服务的应用中,统一的认证授权是必不可少的组件,本文将介绍微服务中网关和子服务如何使用统一的权限认证

主要介绍内容为:

1、子服务如何实现和网关相同的鉴权方式

2、接口权限如何动态配置与修改

3、前后端分离模式下通用的后台管理系统(用户、权限、菜单、平台)

需提前了解知识点:

1、Jwt (JSON Web Token)

2、ClaimsPrincipal

3、Microsoft.AspNetCore.Authorization、AuthorizationPolicy、AuthorizationHandler

子服务和网关鉴权方式

首先我们需要了解一下Ocelot网关权限的使用方式,直接上代码

配置

"AuthenticationOptions": {
    "AuthenticationProviderKey": "TestKey",
    "AllowedScopes": ["admin","user"]
}

认证

复制代码
 var result = await context.HttpContext.AuthenticateAsync(context.DownstreamReRoute.AuthenticationOptions.AuthenticationProviderKey);
 context.HttpContext.User = result.Principal;
 if (context.HttpContext.User.Identity.IsAuthenticated)
 {await _next.Invoke(context);
 }
复制代码

权限验证

var authorised = _scopesAuthoriser.Authorise(context.HttpContext.User, context.DownstreamReRoute.AuthenticationOptions.AllowedScopes);

从以前的代码我们可以看出认证授权的逻辑

1、HttpContext.AuthenticateAsync来进行认证验证,即验证Jwt token的有效可用性,其中AuthenticationProviderKey为身份验证提供程序标识,例如

public void ConfigureServices(IServiceCollection services) { var authenticationProviderKey = "TestKey"; services.AddAuthentication().AddJwtBearer(authenticationProviderKey, x => { }); }

2、当1验证通过后,我们可以通过context.HttpContext.User获取key为scope的Claim数组信息(所以token生成要带上此参数),然后与配置的AllowedScopes的数组进行交集验证,当交集大于0时即为有权限访问

所以子服务如果需要实现和网关相同的权限验证就需要实现以上的方式,用过net core默认的权限认证时会发现,权限的验证都需要体现设定好接口的可访问角色等参数,这不符合我们的需求所以我们需要实现一个自定义的权限认证AuthorizationHandler,直接上代码:

  View Code

其中_permissionAuthoriser.Authorise为权限验证方法,继续往下看实现逻辑

复制代码
 1  public class ScopesAuthoriser : IPermissionAuthoriser
 2     {
 3         private readonly IPermissionRepository _permissionRepository;
 4         private readonly IClaimsParser _claimsParser;
 5         private readonly string _scope = "scope";
 6         private bool _loaded = false;
 7         public ScopesAuthoriser(IPermissionRepository permissionRepository, IClaimsParser claimsParser)
 8         {
 9             _permissionRepository = permissionRepository;
10             _claimsParser = claimsParser;
11         }
12 
13         public bool Authorise(HttpContext httpContext)
14         {
15             if (!_loaded && _permissionRepository.Permissions.Count == 0)
16                 _permissionRepository.Get();
17             _loaded = true;
18 
19             var permission = _permissionRepository.Permissions
20                 .FirstOrDefault(it => string.Equals(it.Path, httpContext.Request.Path, StringComparison.CurrentCultureIgnoreCase) && it.Method == httpContext.Request.Method);
21 
22             if (permission == null)
23                 return true;
24 
25             var values = _claimsParser.GetValuesByClaimType(httpContext.User.Claims, _scope);
26 
27             var matchesScopes = permission.Scope.Intersect(values).ToList();
28 
29             if (matchesScopes.Count == 0)
30                 return false;
31 
32             return true;
33         }
34     }
复制代码

其中_permissionRepository.Permissions是应用的接口列表与接口对应的可访问scope;权限仓储下面进行介绍

接口权限如何动态配置与修改

认证授权数据库设计,tb_api_resources Api资源表、tb_roles 角色表、tb_role_apis 角色Api资源关系表、tb_users 用户表、tb_user_roles 用户角色表

常规验证权限方式,是根据用户的id查询用户角色,然后验证角色是否拥有接口权限;而在网关中是反过来该接口有哪些角色可以访问;

所以我们需要初始化出应用接口对应所需角色,目前我们实现了mysql版本的权限仓储IPermissionRepository的数据查询,代码如下

 

复制代码
 1 public class MySqlPermissionRepository : IPermissionRepository
 2     {
 3         private readonly string _dbConnectionString;
 4         private readonly string _projectName;
 5 
 6         public MySqlPermissionRepository(string dbConnectionString, string projectName)
 7         {
 8             _dbConnectionString = dbConnectionString;
 9             _projectName = projectName;
10         }
11         public List<Permission> Permissions { get; private set; } = new List<Permission>();
12         public async Task Get()
13         {
14             using (var dbContext = new MySqlConnection(_dbConnectionString))
15             {
16                 // 平台下所有需要认证Scope的接口
17                 var apiList = await dbContext.QueryAsync<ApiInfo>(@"SELECT api.Url,api.Method,roleapi.RoleId
18                                                                     FROM tb_api_resources AS api 
19                                                                     LEFT JOIN tb_role_apis AS roleapi ON api.Id = roleapi.ApiId
20                                                                     WHERE AllowScope = 2 AND ProjectName = @ProjectName", new { ProjectName = _projectName });
21                 // 所有角色
22                 var roleList = await dbContext.QueryAsync<RoleInfo>(@"SELECT Id, `Key` from tb_roles WHERE IsDel=0", new { ProjectName = _projectName });
23                 if (apiList.Any())
24                 {
25                     var permission = new List<Permission>();
26                     var apiUrlList = apiList.GroupBy(it => it.Url).Select(it => it.FirstOrDefault()).ToList();
27                     apiUrlList.ForEach(api =>
28                     {
29                         var apiMethodList = apiList.Where(it => it.Url == api.Url).GroupBy(it => it.Method).Select(it => it.FirstOrDefault()).ToList();
30                         apiMethodList.ForEach(method =>
31                         {
32                             var apiInfo = apiList.Where(it => it.Url == api.Url && it.Method == method.Method).FirstOrDefault();
33                             var roleids = apiList.Where(it => it.Url == api.Url && it.Method == method.Method).Select(it => it.RoleId).ToArray();
34                             var scopes = roleList.Where(it => roleids.Contains(it.Id)).Select(it => it.Key).ToList();
35                             permission.Add(new Permission
36                             {
37                                 Path = apiInfo.Url,
38                                 Method = apiInfo.Method,
39                                 Scope = scopes
40                             });
41                         });
42                     });
43                     if (permission.Count > 0)
44                         Permissions = permission;
45                 }
46             }
47         }
48     }
复制代码

这里只会实现一次查询,如果中间有接口权限进行了修改,那么如何进行更新呢,在上一篇配置中间使用中,我们介绍了如何使用组件的定时任务和组件的监听方式,所以我们只需做对应扩展即可,定时代码就不贴了,监听代码如下:

复制代码
 1 public class BucketAuthorizeListener : IBucketListener
 2     {
 3         private readonly IPermissionRepository _permissionRepository;
 4 
 5         public BucketAuthorizeListener(IPermissionRepository permissionRepository)
 6         {
 7             _permissionRepository = permissionRepository;
 8         }
 9 
10         public string ListenerName => "Bucket.Authorize";
11 
12         public async Task ExecuteAsync(string commandText)
13         {
14             if (!string.IsNullOrWhiteSpace(commandText) && commandText == NetworkCommandType.Reload.ToString())
15                 await _permissionRepository.Get();
16         }
17     }
复制代码

下面贴一下Bucket.Authorize如何使用代码

复制代码
1         public void ConfigureServices(IServiceCollection services)
2         {
3             // 添加授权
4             services.AddApiJwtAuthorize(Configuration);
5             // 添加授权认证
6             services.AddApiJwtAuthorize(Configuration).UseAuthoriser(services, builder => { builder.UseMySqlAuthorize(); });
7         }
复制代码

然后在需要认证授权的action或者controller加上[Authorize("permission")]属性,appsetting配置如下,也可移至配置中心

  View Code

前后端分离模式下通用的后台管理系统

在FamilyBucket-UI中我们可以对项目的接口权限认证方式、用户、用户角色、角色、角色权限、角色菜单等等进行配置,

同时FamilyBucket-UI还具有通用管理系统的基础模块,里面增加一个管理平台的概念,可以直接多个管理平台使用同一个用户体系

本章就不做介绍了,内容有点长,下次再做详细介绍,在多平台同时管理时项目还需要进行一些升级,截图如下

本章涉及源码均可在github中进行查看

https://github.com/q315523275/FamilyBucket

https://github.com/q315523275/FamilyBucket-UI 

原文地址https://www.cnblogs.com/tianxiangzhe/p/10419334.html

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
项目管理 微服务
云效常见问题之将多个微服务应用集成到一次研发流程中发布上线如何解决
云效(CloudEfficiency)是阿里云提供的一套软件研发效能平台,旨在通过工程效能、项目管理、质量保障等工具与服务,帮助企业提高软件研发的效率和质量。本合集是云效使用中可能遇到的一些常见问题及其答案的汇总。
28 0
|
1月前
|
数据库 Android开发 开发者
构建高性能微服务架构:从理论到实践构建高效Android应用:探究Kotlin协程的优势
【2月更文挑战第16天】 在当今快速迭代和竞争激烈的软件市场中,微服务架构以其灵活性、可扩展性和独立部署能力而受到企业的青睐。本文将深入探讨如何构建一个高性能的微服务系统,涵盖从理论基础到具体实现的各个方面。我们将重点讨论服务拆分策略、通信机制、数据一致性以及性能优化等关键主题,为读者提供一个清晰、实用的指南,以便在复杂多变的业务环境中构建和维护健壮的微服务体系结构。 【2月更文挑战第16天】 在移动开发领域,性能优化和流畅的用户体验是至关重要的。随着技术的不断进步,Kotlin作为一种现代编程语言,在Android开发中被广泛采用,尤其是其协程特性为异步编程带来了革命性的改进。本文旨在深入
241 5
|
1月前
|
监控 网络协议 Go
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
109646 118
|
7天前
|
Kubernetes Cloud Native Go
《Go 简易速速上手小册》第10章:微服务与云原生应用(2024 最新版)(下)
《Go 简易速速上手小册》第10章:微服务与云原生应用(2024 最新版)
41 0
|
7天前
|
Cloud Native 算法 Go
《Go 简易速速上手小册》第10章:微服务与云原生应用(2024 最新版)(上)
《Go 简易速速上手小册》第10章:微服务与云原生应用(2024 最新版)
32 0
|
11天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
13天前
|
Kubernetes 监控 Cloud Native
构建高效云原生应用:基于Kubernetes的微服务治理实践
【4月更文挑战第13天】 在当今数字化转型的浪潮中,企业纷纷将目光投向了云原生技术以支持其业务敏捷性和可扩展性。本文深入探讨了利用Kubernetes作为容器编排平台,实现微服务架构的有效治理,旨在为开发者和运维团队提供一套优化策略,以确保云原生应用的高性能和稳定性。通过分析微服务设计原则、Kubernetes的核心组件以及实际案例,本文揭示了在多变的业务需求下,如何确保系统的高可用性、弹性和安全性。
17 4
|
18天前
|
SpringCloudAlibaba Java Nacos
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
|
1月前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
785 0
|
1月前
|
微服务
SpringCloud-Config服务端微服务从自己的Gitee上获取配置内容配置读取规则
SpringCloud-Config服务端微服务从自己的Gitee上获取配置内容配置读取规则
17 0