(精华)2020年10月21日 设计模式案例 .NetCore Middleware实现原理

it2024-11-07  6

Middleware使用

#region Middleware使用 { // 1、创建IApplicationBuilder IApplicationBuilder applicationBuilder = new IApplicationBuilder(); applicationBuilder.UseAuthentication(); applicationBuilder.UseAuthorization(); applicationBuilder.UseException(); // 2、构建中间件 Middleware middleware = applicationBuilder.Build(); // 3、执行中间件 HttpContext httpContext = new HttpContext(); httpContext.httpRequest = new HttpRequest() { requestUrl = "index.html" }; middleware.HandlerRequest(httpContext); } #endregion

Application应用相关类

/// <summary> /// aspnetcore 应用程序 /// </summary> class IApplicationBuilder { /// <summary> /// 应用程序构建 /// </summary> private IApplication application = new IApplication(); /// <summary> /// 1、构建异常中间件 /// </summary> public IApplicationBuilder UseException() { // 1、创建异常中间件 Middleware middleware = new ExceptionMiddleware(); // 2、添加到应用程序中 application.middlewares.Add(middleware); return this; } /// <summary> /// 2、构建身份认证中间件 /// </summary> public IApplicationBuilder UseAuthentication() { // 1、创建身份认证中间件 Middleware middleware = new AuthenticationMiddleware(); // 2、添加到应用程序中 application.middlewares.Add(middleware); return this; } /// <summary> /// 3、构建授权中间件 /// </summary> public IApplicationBuilder UseAuthorization() { // 1、创建身份认证中间件 Middleware middleware = new AuthorizationMiddleware(); // 2、添加到应用程序中 application.middlewares.Add(middleware); return this; } /// <summary> /// 构建中间件(按照责任链形式添加) /// </summary> /// <returns></returns> public Middleware Build() { // 1、创建默认404中间件 Middleware middlewareFrist = new Default404Middleware(); // 2、遍历中间件添加到后面 List<Middleware> middlewares = application.middlewares; middlewares.Reverse(); foreach (Middleware middleware in middlewares) { // 1、middlewareFrist添加下一个中间件 middlewareFrist.nextMiddleware = middleware; // 2、将默认中间件进行替换 middlewareFrist = middleware; } // 3、返回中间件 return middlewareFrist; } } /// <summary> /// aspnetcore 应用程序 /// </summary> class IApplication { // 中间件集合 public List<Middleware> middlewares { set; get; } = new List<Middleware>(); }

权限验证相关类

/// <summary> /// cookie身份认证 /// </summary> class CookieAuthentication : IAuthentication { // 1、缓存用户名(集合) === foreach 迭代器模式 public void Authentica(HttpContext httpContext) { Console.WriteLine($"Cookie身份认证"); // `1、查询身份信息(从数据库) (非常多用户名) } } /// <summary> /// 身份认证接口 /// </summary> interface IAuthentication { /// <summary> /// 认证身份 /// </summary> /// <param name="httpContext"></param> public void Authentica(HttpContext httpContext); } /// <summary> /// Jwt身份认证 /// </summary> class JwtIAuthentication : IAuthentication { public void Authentica(HttpContext httpContext) { Console.WriteLine($"Jwt身份认证"); } } /// <summary> /// 空对象身份认证 /// </summary> class NullAuthentication : IAuthentication { public void Authentica(HttpContext httpContext) { Console.WriteLine($"身份认证不存在"); } }

相关中间件

/// <summary> /// 身份认证中间件 /// </summary> class AuthenticationMiddleware : Middleware { /// <summary> /// 中间件是否终止条件 /// </summary> private string token = "authen_token"; public override void HandlerRequest(HttpContext httpContext) { Console.WriteLine($"执行身份认证中间件"); // 1、判断请求url是否包含token,包含则终止 if (httpContext.httpRequest.requestUrl.Contains(token)) { Console.WriteLine($"身份认证中间件终止了"); // 1、Cookie // 2、JWT // 3、IdentityServer4 // 3、选择身份认证程序程序(Cookie2) null 空指针异常 return; } else { // 2、执行下一个中间件 nextMiddleware.HandlerRequest(httpContext); } } } /// <summary> /// 授权中间件 /// </summary> class AuthorizationMiddleware : Middleware { /// <summary> /// 中间件是否终止条件 /// </summary> private string token = "author_token"; public override void HandlerRequest(HttpContext httpContext) { Console.WriteLine($"执行授权中间件"); // 1、判断请求url是否包含token,包含则终止 if (httpContext.httpRequest.requestUrl.Contains(token)) { Console.WriteLine($"授权中间件终止了"); return; } else { // 2、执行下一个中间件 nextMiddleware.HandlerRequest(httpContext); } } } /// <summary> /// 默认404中间件 /// </summary> class Default404Middleware : Middleware { /// <summary> /// 中间件是否终止条件 /// </summary> private string token = "404"; public override void HandlerRequest(HttpContext httpContext) { Console.WriteLine($"执行默认404中间件"); // 1、判断请求url是否包含token,包含则终止 if (httpContext.httpRequest.requestUrl.Contains(token)) { Console.WriteLine($"默认40中间件终止了"); return; } else { // 2、执行下一个中间件 nextMiddleware.HandlerRequest(httpContext); } } } /// <summary> /// 异常中间件 /// </summary> class ExceptionMiddleware : Middleware { /// <summary> /// 中间件是否终止条件 /// </summary> private string token = "exception"; public override void HandlerRequest(HttpContext httpContext) { Console.WriteLine($"执行异常中间件"); // 1、判断请求url是否包含token,包含则终止 if (httpContext.httpRequest.requestUrl.Contains(token)) { Console.WriteLine($"异常中间件终止了"); return; } else { // 2、执行下一个中间件 nextMiddleware.HandlerRequest(httpContext); } } } /// <summary> /// 中间件类(用来处理http请求) /// </summary> abstract class Middleware { // 下一个中间件 public Middleware nextMiddleware { set; get; } /// <summary> /// 处理请求 /// </summary> /// <param name="request"></param> public abstract void HandlerRequest(HttpContext httpContext); }

HTTP相关类

/// <summary> /// http上下文类(用于获取HttpRequest和HttpResponse) /// </summary> class HttpContext { public HttpRequest httpRequest { set; get; } // 请求输入类 public HttpResponse httpResponse { set; get; } // 请求响应输入 } /// <summary> /// http请求类 /// </summary> class HttpRequest { /// <summary> /// 请求url /// </summary> public string requestUrl; } /// <summary> /// http响应类 /// </summary> class HttpResponse { }
最新回复(0)