(精华)2020年10月21日 设计模式案例 .NetCore Middleware实现原理
【摘要】
Middleware使用
#region Middleware使用
{
// 1、创建IApplicationBuilder
IApplicationBuilder applicatio...
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
/// <summary>
/// aspnetcore 应用程序
/// </summary>
class IApplication
{
// 中间件集合
public List<Middleware> middlewares { set; get; } = new List<Middleware>();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
权限验证相关类
/// <summary>
/// cookie身份认证
/// </summary>
class CookieAuthentication : IAuthentication
{
// 1、缓存用户名(集合) === foreach 迭代器模式
public void Authentica(HttpContext httpContext)
{
Console.WriteLine($"Cookie身份认证");
// `1、查询身份信息(从数据库) (非常多用户名)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
/// <summary>
/// 身份认证接口
/// </summary>
interface IAuthentication
{
/// <summary>
/// 认证身份
/// </summary>
/// <param name="httpContext"></param>
public void Authentica(HttpContext httpContext);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
/// <summary>
/// Jwt身份认证
/// </summary>
class JwtIAuthentication : IAuthentication
{
public void Authentica(HttpContext httpContext)
{
Console.WriteLine($"Jwt身份认证");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
/// <summary>
/// 空对象身份认证
/// </summary>
class NullAuthentication : IAuthentication
{
public void Authentica(HttpContext httpContext)
{
Console.WriteLine($"身份认证不存在");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
相关中间件
/// <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);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
/// <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);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
/// <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);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
/// <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);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
/// <summary>
/// 中间件类(用来处理http请求)
/// </summary>
abstract class Middleware
{
// 下一个中间件
public Middleware nextMiddleware { set; get; }
/// <summary>
/// 处理请求
/// </summary>
/// <param name="request"></param>
public abstract void HandlerRequest(HttpContext httpContext);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
HTTP相关类
/// <summary>
/// http上下文类(用于获取HttpRequest和HttpResponse)
/// </summary>
class HttpContext
{
public HttpRequest httpRequest { set; get; } // 请求输入类
public HttpResponse httpResponse { set; get; } // 请求响应输入
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
/// <summary>
/// http请求类
/// </summary>
class HttpRequest
{
/// <summary>
/// 请求url
/// </summary>
public string requestUrl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
/// <summary>
/// http响应类
/// </summary>
class HttpResponse
{
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/109203508
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)