目录

  • 开发任务
  • 代码实现

开发任务

  • DotNetNB.Security.Core:定义 core,models,Istore;实现 default memory store
  • DotNetNB.Security.EntityAccess:扫描 entities;添加 ef savechanges interceptor

代码实现

我们现在已经通过 ActionResourceProvider 完成了 action 的扫描,生成了 ResourceModel,需要持久化到 IResourceStore,持久化之后才可以将它们绑定到用户,角色

由于 ActionAccess 是一个类库,提供了一些比较零散的功能,所以需要添加一个扩展方法把功能组装起来,在 host 启动的时候执行 action 的扫描

using Microsoft.Extensions.DependencyInjection;

namespace DotNetNB.Security.Core.Extensions
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSecurity(this IServiceCollection services)
{
services.AddHostedService<ResourceProviderHostedService>();
return services;
}
}
}

ResourceProviderHostedService 继承自 IHostedService,有一个 StartAsync 和一个 StopAsync 方法

using Microsoft.Extensions.Hosting;

namespace DotNetNB.Security.Core
{
public class ResourceProviderHostedService : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{ } public async Task StopAsync(CancellationToken cancellationToken)
{ }
}
}

新建一个示例的 api 项目 DotNetNB.WebApplication,在这个 api 项目里面使用我们的 dll 要足够简单,就像使用 asp .net core 的 api 一样

添加 DotNetNB.Security.Core 的项目引用之后,可以直接在 Program.cs 中调用扩展方法

using DotNetNB.Security.Core.Extensions;

...

builder.Services.AddSecurity();

在启动扫描的时候,Security.Core 并不知道外部的 host 里面有哪些 action provider,所以需要注册进来,需要构建一个 builder

同时需要一个配置 options 告诉我们它是来自哪个包,是 ActionAccess,还是 EntityAccess

参照 MvcOptions

builder.Services.AddControllers(options => {});

它是一个 Action 的委托

public static IMvcBuilder AddControllers(
this IServiceCollection services,
Action<MvcOptions>? configure)
{
IMvcCoreBuilder builder = services != null ? MvcServiceCollectionExtensions.AddControllersCore(services) : throw new ArgumentNullException(nameof (services));
if (configure != null)
builder.AddMvcOptions(configure);
return (IMvcBuilder) new MvcBuilder(builder.Services, builder.PartManager);
}

于是乎我们在 AddSecurity 添加一个入参

public static IServiceCollection AddSecurity(this IServiceCollection services, Action<SecurityOption>? configure)

SecurityOption

using Microsoft.Extensions.DependencyInjection;

namespace DotNetNB.Security.Core.Extensions
{
public class SecurityOption
{
public IServiceCollection Services { get; set; }
}
}

在调用 AddSecurity 扩展方法的时候通过 SecurityOption 进行配置,这样所有对外的 api 只需要做这一个配置就可以把两个包的所有功能引用进去

builder.Services.AddSecurity(options =>
{
options.AddActionAccess();
options.AddEntityAccess<DBContext>();
});

参考 MvcCoreServiceCollectionExtensions 的 AddMvcCoreServices 方法

internal static void AddMvcCoreServices(IServiceCollection services)
{
//
// Options
//
services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcCoreMvcOptionsSetup>()); ...
}

在 ActionAccess 中添加一个扩展方法 AddActionAccessControl,将 IResourceProvider 添加进去,这样就可以在 ResourceProviderHostedService 中读取到

using DotNetNB.Security.Core;
using DotNetNB.Security.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; namespace DotNetNB.Security.ActionAccess
{
public static class SecurityOptionExtensions
{
public static SecurityOption AddActionAccessControl(this SecurityOption option)
{
option.Services.TryAddEnumerable(ServiceDescriptor.Transient<IResourceProvider, ActionResourceProvider>());
return option;
}
}
}

在 ResourceProviderHostedService 的构造函数中读取 IServiceProvider

using Microsoft.Extensions.Hosting;

namespace DotNetNB.Security.Core
{
public class ResourceProviderHostedService : IHostedService
{
private readonly IServiceProvider[] _serviceProviders; public ResourceProviderHostedService(IServiceProvider[] serviceProviders)
{
_serviceProviders = serviceProviders;
} public async Task StartAsync(CancellationToken cancellationToken)
{ } public async Task StopAsync(CancellationToken cancellationToken)
{ }
}
}

在 EntityAccess 中同样添加一个扩展方法 AddEntityAccessControl

using DotNetNB.Security.Core;
using DotNetNB.Security.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; namespace DotNetNB.Security.EntityAccess
{
public static class SecurityOptionExtensions
{
public static SecurityOption AddEntityAccessControl(this SecurityOption option)
{
option.Services.TryAddEnumerable(ServiceDescriptor.Transient<IResourceProvider, EntityResourceProvider>());
return option;
}
}
}

EntityResourceProvider 继承 IResourceProvider

using DotNetNB.Security.Core;
using DotNetNB.Security.Core.Models; namespace DotNetNB.Security.EntityAccess
{
public class EntityResourceProvider : IResourceProvider
{
public async Task<IEnumerable<Resource>> ExecuteAsync()
{
return new List<Resource>();
}
}
}

完成之后在 DotNetNB.WebApplication 中添加项目引用,就可以进行配置

builder.Services.AddSecurity(options =>
{
options.AddActionAccessControl()
.AddEntityAccessControl();
});

后面再完善 AddEntityAccessControl 加入 DBContext

GitHub源码链接:

https://github.com/MingsonZheng/dotnetnb.security

课程链接

https://appsqsyiqlk5791.h5.xiaoeknow.com/v1/course/video/v_5f39bdb8e4b01187873136cf?type=2

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

.NET 云原生架构师训练营(权限系统 代码实现 EntityAccess)--学习笔记的更多相关文章

  1. .NET 云原生架构师训练营(建立系统观)--学习笔记

    目录 目标 ASP .NET Core 什么是系统 什么是系统思维 系统分解 什么是复杂系统 作业 目标 通过整体定义去认识系统 通过分解去简化对系统的认识 ASP .NET Core ASP .NE ...

  2. .NET 云原生架构师训练营(对象过程建模)--学习笔记

    目录 UML OPM OPM优化 UML 1997年发布UML标准 主要域 视图 图 主要概念 结构 静态视图 类图 类.关联.泛化.依赖关系.实现.接口 用例视图 用例图 用例.参与者.关联.扩展. ...

  3. .NET 云原生架构师训练营(设计原则&&设计模式)--学习笔记

    目录 设计原则 设计模式 设计原则 DRY (Don't repeat yourself 不要重复) KISS (Keep it stupid simple 简单到傻子都能看懂) YAGNI (You ...

  4. .NET 云原生架构师训练营(责任链模式)--学习笔记

    目录 责任链模式 源码 责任链模式 职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无需关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了 何时使用:在处理 ...

  5. .NET 云原生架构师训练营(系统架构)--学习笔记

    目录 对外展现的功能 内部功能 功能交互与价值通路 系统架构 目标 认识系统的价值通路 认识功能架构,通过把功能结构与形式结构结合来描述系统架构 受益原则 好的架构必须使人受益,要想把架构做好,就要专 ...

  6. .NET 云原生架构师训练营(模块一 架构师与云原生)--学习笔记

    目录 什么是软件架构 软件架构的基本思路 单体向分布式演进.云原生.技术中台 1.1 什么是软件架构 1.1.1 什么是架构? Software architecture = {Elements, F ...

  7. .NET 云原生架构师训练营(权限系统 RGCA 架构设计)--学习笔记

    目录 项目核心内容 实战目标 RGCA 四步架构法 项目核心内容 无代码埋点实现对所有 API Action 访问控制管理 对 EF Core 实体新增.删除.字段级读写控制管理 与 Identity ...

  8. .NET 云原生架构师训练营(权限系统 RGCA 开发任务)--学习笔记

    目录 目标 模块拆分 OPM 开发任务 目标 基于上一讲的模块划分做一个任务拆解,根据任务拆解实现功能 模块拆分 模块划分已经完成了边界的划分,边界内外职责清晰 OPM 根据模块拆分画出 OPM(Ob ...

  9. .NET 云原生架构师训练营(权限系统 代码实现 ActionAccess)--学习笔记

    目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...

  10. .NET 云原生架构师训练营(权限系统 代码实现 Identity)--学习笔记

    目录 开发任务 代码实现 开发任务 DotNetNB.Security.Core:定义 core,models,Istore:实现 default memory store DotNetNB.Secu ...

随机推荐

  1. JMeter_使用正则和JSON提取器参数化(常用于提取token)

    一.使用正则表达式提取器提取token 查看登录响应参数找出token.图中token为 "ticketString": "ccf26b17-a96f-4913-8925 ...

  2. spring-aop(二)学习笔记

    常用增强处理类型 增强处理类型                                                        特点 before 前置增强处理,在目标方法前织入增强处理 ...

  3. Windows 10 如何在当前位置打开 CMD 命令窗口?

    方法一 Win + R 键召唤出运行窗口,然后输入 "CMD" 打开命令提示符. 使用 cd 命令更改当前命令提示符的工作环境. 注释 cd/ - 退到当前所在盘符 cd.. - ...

  4. Go的日志库go-logging

    配置文件config.yaml log: prefix: '[MY-LOG] ' log-file: true stdout: 'DEBUG' file: 'DEBUG' config/config. ...

  5. Spring Security OAuth2 完全解析 (流程/原理/实战定制) —— Client / ResourceServer 篇

    一.前言 本文假设读者对 Spring Security 本身原理有一定程度的了解,假设对 OAuth2 规范流程.Jwt 有基础了解,以此来对 SpringSecurity 整合 OAuth2 有个 ...

  6. 万字总结Keras深度学习中文文本分类

    摘要:文章将详细讲解Keras实现经典的深度学习文本分类算法,包括LSTM.BiLSTM.BiLSTM+Attention和CNN.TextCNN. 本文分享自华为云社区<Keras深度学习中文 ...

  7. 【机器学习】GMM和EM算法

    机器学习算法-GMM和EM算法 目录 机器学习算法-GMM和EM算法 1. GMM模型 2. GMM模型参数求解 2.1 参数的求解 2.2 参数和的求解 3. GMM算法的实现 3.1 gmm类的定 ...

  8. WSL与gnome-desktop

    WSL与gome-desktop 经过测试和检索 确定WSL1无法在gome-desktop实现GUI桌面 只能实现其中应用的现实,比如打开记事本在Xserver https://www.reddit ...

  9. Git:解决报错:fatal: The remote end hung up unexpectedly

    使用全局代理即可.字面意思连接时间过长,被github中断了连接.

  10. 乡亲们,我们创建了 Dapr 中文交流频道

    我们创建了 Dapr 中文交流 QQ 频道,欢迎大家加入!加入方式在文章最后一节. 为什么要创建频道? 解决什么问题 专业性,"你可以在我们群里面钓鱼,因为都是水" 你肯定加过非常 ...