授权中间件的介绍

重返目录

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相互调用的气象相比有用

响合时间中间件

笔录大于指按期期的哀求新闻,方便做质量完全的进级
回到目录

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

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。