授权中间件的介绍

2019-10-17 22:33 来源:未知

回来目录

LindDotNetCore中间件

父辈认知中间件就是任重(Ren Zhong)而道远对http央求举办阻挠,然后增多具体天性化功效的逻辑,这种把诉求切开,增添新逻辑的方法相似称为面向方面包车型的士逻辑AOP!

  1. 授权中间件
  2. 恳求链追踪中间件
  3. 响合时间中间件

    #### 授权中间件

    伸手有效性的校验

  • 授权参数

     /// <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相互调用的场景相比有用

响适当时候间中间件

记录大于指定时期的伸手音信,方便做品质完全的进级
回来目录

TAG标签:
版权声明:本文由银河国际点击登录发布于手机客户端,转载请注明出处:授权中间件的介绍