From 3d22a2ef657e29c68f57ea97872d7de2568cc890 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sun, 29 Jun 2025 19:34:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=89=B4=E6=9D=83=E5=88=B7=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RefreshAuthenticationHandlerProvider.cs | 64 +++++++++++++++++++ .../SwaggerAddExtensions.cs | 5 -- .../YiFrameworkAspNetCoreModule.cs | 25 +++----- .../AuthenticationExtensions.cs | 18 ++++++ .../IRefreshAuthenticationHandlerProvider.cs | 11 ++++ .../Authorization/RefreshTokenMiddleware.cs | 11 ++-- 6 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Authentication/RefreshAuthenticationHandlerProvider.cs create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/AuthenticationExtensions.cs create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/IRefreshAuthenticationHandlerProvider.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Authentication/RefreshAuthenticationHandlerProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Authentication/RefreshAuthenticationHandlerProvider.cs new file mode 100644 index 00000000..1842a66e --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/AspNetCore/Authentication/RefreshAuthenticationHandlerProvider.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; +using Yi.Framework.Core.Authentication; + +namespace Yi.Framework.AspNetCore.Microsoft.AspNetCore.Authentication; + +/// +/// 可刷新的鉴权提供者 +/// +public class RefreshAuthenticationHandlerProvider : IRefreshAuthenticationHandlerProvider +{ + private Dictionary _handlerMap = + new Dictionary((IEqualityComparer)StringComparer.Ordinal); + + /// Constructor. + /// The . + public RefreshAuthenticationHandlerProvider(IAuthenticationSchemeProvider schemes) + { + this.Schemes = schemes; + } + + /// + /// The . + /// + public IAuthenticationSchemeProvider Schemes { get; } + + /// Returns the handler instance that will be used. + /// The context. + /// The name of the authentication scheme being handled. + /// The handler instance. + public async Task GetHandlerAsync( + HttpContext context, + string authenticationScheme) + { + IAuthenticationHandler handlerAsync; + if (this._handlerMap.TryGetValue(authenticationScheme, out handlerAsync)) + return handlerAsync; + AuthenticationScheme schemeAsync = await this.Schemes.GetSchemeAsync(authenticationScheme); + if (schemeAsync == null) + return (IAuthenticationHandler)null; + + if ((context.RequestServices.GetService(schemeAsync.HandlerType) ?? + ActivatorUtilities.CreateInstance(context.RequestServices, schemeAsync.HandlerType)) is + IAuthenticationHandler handler) + { + handlerAsync = handler; + await handler.InitializeAsync(schemeAsync, context); + this._handlerMap[authenticationScheme] = handler; + } + + return handlerAsync; + } + + + /// + /// 刷新鉴权 + /// + public void RefreshAuthentication() + { + _handlerMap = new Dictionary((IEqualityComparer)StringComparer.Ordinal); + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs index b6fe3861..0ccdb94d 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/Microsoft/Extensions/DependencyInjection/SwaggerAddExtensions.cs @@ -1,17 +1,12 @@ using System.ComponentModel; -using System.Diagnostics; using System.Text; -using System.Xml.Linq; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.Conventions; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Options; namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection { diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs index 36cdb0d9..eeca981c 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs @@ -1,21 +1,10 @@ -using System.Reflection; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.ApiExplorer; -using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Options; -using Microsoft.OpenApi.Models; -using Newtonsoft.Json.Linq; -using Swashbuckle.AspNetCore.SwaggerGen; -using Volo.Abp; -using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.WebClientInfo; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Modularity; -using Yi.Framework.AspNetCore.Mvc; +using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Authentication; using Yi.Framework.Core; +using Yi.Framework.Core.Authentication; namespace Yi.Framework.AspNetCore { @@ -35,8 +24,14 @@ namespace Yi.Framework.AspNetCore // 替换默认的WebClientInfoProvider为支持代理的实现 services.Replace(new ServiceDescriptor( typeof(IWebClientInfoProvider), - typeof(RealIpHttpContextWebClientInfoProvider), + typeof(RealIpHttpContextWebClientInfoProvider), ServiceLifetime.Transient)); + + // 替换默认的AuthenticationHandlerProvider为支持刷新鉴权 + services.Replace(new ServiceDescriptor( + typeof(IAuthenticationHandlerProvider), + typeof(RefreshAuthenticationHandlerProvider), + ServiceLifetime.Scoped)); } } } \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/AuthenticationExtensions.cs b/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/AuthenticationExtensions.cs new file mode 100644 index 00000000..c2db1599 --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/AuthenticationExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace Yi.Framework.Core.Authentication; + +public static class AuthenticationExtensions +{ + public static void RefreshAuthentication(this HttpContext context) + { + var currentAuthenticationHandler = + context.RequestServices.GetRequiredService(); + if (currentAuthenticationHandler is IRefreshAuthenticationHandlerProvider refreshAuthenticationHandler) + { + refreshAuthenticationHandler.RefreshAuthentication(); + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/IRefreshAuthenticationHandlerProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/IRefreshAuthenticationHandlerProvider.cs new file mode 100644 index 00000000..6cdca24c --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.Core/Authentication/IRefreshAuthenticationHandlerProvider.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Authentication; + +namespace Yi.Framework.Core.Authentication; + +public interface IRefreshAuthenticationHandlerProvider: IAuthenticationHandlerProvider +{ + /// + /// 刷新鉴权 + /// + void RefreshAuthentication(); +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/RefreshTokenMiddleware.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/RefreshTokenMiddleware.cs index 73a8fd1a..fa4050bb 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/RefreshTokenMiddleware.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Authorization/RefreshTokenMiddleware.cs @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Claims; +using Yi.Framework.Core.Authentication; using Yi.Framework.Rbac.Domain.Managers; using Yi.Framework.Rbac.Domain.Shared.Consts; @@ -45,16 +46,12 @@ namespace Yi.Framework.Rbac.Domain.Authorization context.Response.Headers["access_token"] = access_Token; context.Response.Headers["refresh_token"] = refresh_Token; - //请求头替换,补充后续鉴权逻辑 context.Request.Headers["Authorization"] = "Bearer " + access_Token; + + //刷新鉴权状态 + context.RefreshAuthentication(); } - //刷新token 与 access_token都失效了 - // else - // { - //context.Response.StatusCode = StatusCodes.Status401Unauthorized; - //return; - // } } }