当前位置:澳门新萄京网址 > 澳门新萄京网址 > 授权中间件的介绍

授权中间件的介绍

文章作者:澳门新萄京网址 上传时间:2019-11-01

归来目录

LindDotNetCore中间件

伯父认知中间件正是至关重大对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相互调用的风貌比较有用

一呼百诺时间中间件

记录大于指按期期的乞请音信,方便做品质完全的升官
再次回到目录

本文由澳门新萄京网址发布于澳门新萄京网址,转载请注明出处:授权中间件的介绍

关键词: