LindDotNetCore~授权中间件的介绍
LindDotNetCore中间件
大叔认识中间件就是主要对http请求进行拦截,然后添加具体个性化功能的逻辑,这种把请求切开,添加新逻辑的方式一般称为面向方面的逻辑AOP!
- 授权中间件
- 请求链跟踪中间件
- 响应时间中间件
授权中间件
请求有效性的校验
- 授权参数
/// <summary>
/// 授权配置
/// </summary>
public class AuthorizationConfig
{
/// <summary>
/// 统一密钥
/// </summary>
public string EncryptKey { get; set; }
/// <summary>
/// 过期时间秒数
/// </summary>
public int ExpiredSecond { get; set; }
/// <summary>
/// 被授权的app
/// </summary>
public string[] AppList { get; set; }
}
- 客户端请求参数
/// <summary>
/// 从http请求发过来的授权实体
/// </summary>
public class AuthorizationRequestInfo
{
public string ApplicationId { get; set; }
public string Timestamp { get; set; }
public string Sinature { get; set; }
}
- 请求拦截器,处理请求有效性,对app,过期时间,加密方式进行校验
string computeSinature = MD5($"{requestInfo.ApplicationId}-{requestInfo.Timestamp}-{_options.EncryptKey}");
double tmpTimestamp;
if (computeSinature.Equals(requestInfo.Sinature) &&
double.TryParse(requestInfo.Timestamp, out tmpTimestamp))
{
if (ValidateExpired(tmpTimestamp, _options.ExpiredSecond))
{
await ReturnTimeOut(context);
}
else
{
await ValidateApp(context, requestInfo.ApplicationId);
}
}
else
{
await ReturnNotAuthorized(context);
}
- 为开发人员提供友好的扩展方法,用来注册中间件
/// <summary>
/// 注册授权服务-step1
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> for adding services.</param>
/// <param name="configureOptions">A delegate to configure the <see cref="ResponseCompressionOptions"/>.</param>
/// <returns></returns>
public static IServiceCollection AddLindAuthrization(this IServiceCollection services, Action<AuthorizationConfig> configureOptions = null)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
var options = new AuthorizationConfig();
configureOptions?.Invoke(options);
ObjectMapper.MapperTo(options, ConfigFileHelper.Get<AuthorizationConfig>());
services.AddSingleton(options);
return services;
}
/// <summary>
/// 使用授权中间件-step2
/// </summary>
/// <param name="builder"></param>
/// <param name="options"></param>
/// <returns></returns>
public static IApplicationBuilder UseLindAuthrization(this IApplicationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
var options = builder.ApplicationServices.GetService<AuthorizationConfig>();
return builder.UseMiddleware<AuthorizationMiddleware>(options);
}
- 使用授权中间件Startup中注册
// 注册服务
services.AddLindAuthrization(options =>
{
options.EncryptKey = "abc123";
options.ExpiredSecond = 50;
options.AppList = new string[] { "1", "2", "3" };
});
// 注册中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseLindAuthrization();
app.UseMvc();
}
请求链跟踪中间件
记录请求经过的整个过程,对于多api相互调用的场景比较有用
响应时间中间件
记录大于指定时间的请求信息,方便做性能整体的提升
回到目录
合集:
DotNetCore
分类:
.Net Core
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2015-01-09 EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~再续~添加对各只读服务器的心跳检测
2014-01-09 Thrift架构~windows下安装和Hello World及编码引起的错误