feat: 完成支持鉴权刷新功能
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 可刷新的鉴权提供者
|
||||||
|
/// </summary>
|
||||||
|
public class RefreshAuthenticationHandlerProvider : IRefreshAuthenticationHandlerProvider
|
||||||
|
{
|
||||||
|
private Dictionary<string, IAuthenticationHandler> _handlerMap =
|
||||||
|
new Dictionary<string, IAuthenticationHandler>((IEqualityComparer<string>)StringComparer.Ordinal);
|
||||||
|
|
||||||
|
/// <summary>Constructor.</summary>
|
||||||
|
/// <param name="schemes">The <see cref="T:Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider" />.</param>
|
||||||
|
public RefreshAuthenticationHandlerProvider(IAuthenticationSchemeProvider schemes)
|
||||||
|
{
|
||||||
|
this.Schemes = schemes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="T:Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProvider" />.
|
||||||
|
/// </summary>
|
||||||
|
public IAuthenticationSchemeProvider Schemes { get; }
|
||||||
|
|
||||||
|
/// <summary>Returns the handler instance that will be used.</summary>
|
||||||
|
/// <param name="context">The context.</param>
|
||||||
|
/// <param name="authenticationScheme">The name of the authentication scheme being handled.</param>
|
||||||
|
/// <returns>The handler instance.</returns>
|
||||||
|
public async Task<IAuthenticationHandler?> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新鉴权
|
||||||
|
/// </summary>
|
||||||
|
public void RefreshAuthentication()
|
||||||
|
{
|
||||||
|
_handlerMap = new Dictionary<string, IAuthenticationHandler>((IEqualityComparer<string>)StringComparer.Ordinal);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,12 @@
|
|||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml.Linq;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using Microsoft.OpenApi.Any;
|
using Microsoft.OpenApi.Any;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
using Volo.Abp.AspNetCore.Mvc;
|
using Volo.Abp.AspNetCore.Mvc;
|
||||||
using Volo.Abp.AspNetCore.Mvc.Conventions;
|
using Volo.Abp.AspNetCore.Mvc.Conventions;
|
||||||
using Volo.Abp.DependencyInjection;
|
|
||||||
using Volo.Abp.Options;
|
|
||||||
|
|
||||||
namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
|
namespace Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,21 +1,10 @@
|
|||||||
using System.Reflection;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
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.AspNetCore.WebClientInfo;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Yi.Framework.AspNetCore.Microsoft.AspNetCore.Authentication;
|
||||||
using Volo.Abp.Modularity;
|
|
||||||
using Yi.Framework.AspNetCore.Mvc;
|
|
||||||
using Yi.Framework.Core;
|
using Yi.Framework.Core;
|
||||||
|
using Yi.Framework.Core.Authentication;
|
||||||
|
|
||||||
namespace Yi.Framework.AspNetCore
|
namespace Yi.Framework.AspNetCore
|
||||||
{
|
{
|
||||||
@@ -35,8 +24,14 @@ namespace Yi.Framework.AspNetCore
|
|||||||
// 替换默认的WebClientInfoProvider为支持代理的实现
|
// 替换默认的WebClientInfoProvider为支持代理的实现
|
||||||
services.Replace(new ServiceDescriptor(
|
services.Replace(new ServiceDescriptor(
|
||||||
typeof(IWebClientInfoProvider),
|
typeof(IWebClientInfoProvider),
|
||||||
typeof(RealIpHttpContextWebClientInfoProvider),
|
typeof(RealIpHttpContextWebClientInfoProvider),
|
||||||
ServiceLifetime.Transient));
|
ServiceLifetime.Transient));
|
||||||
|
|
||||||
|
// 替换默认的AuthenticationHandlerProvider为支持刷新鉴权
|
||||||
|
services.Replace(new ServiceDescriptor(
|
||||||
|
typeof(IAuthenticationHandlerProvider),
|
||||||
|
typeof(RefreshAuthenticationHandlerProvider),
|
||||||
|
ServiceLifetime.Scoped));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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<IAuthenticationHandlerProvider>();
|
||||||
|
if (currentAuthenticationHandler is IRefreshAuthenticationHandlerProvider refreshAuthenticationHandler)
|
||||||
|
{
|
||||||
|
refreshAuthenticationHandler.RefreshAuthentication();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
|
||||||
|
namespace Yi.Framework.Core.Authentication;
|
||||||
|
|
||||||
|
public interface IRefreshAuthenticationHandlerProvider: IAuthenticationHandlerProvider
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 刷新鉴权
|
||||||
|
/// </summary>
|
||||||
|
void RefreshAuthentication();
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Builder;
|
|||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Volo.Abp.DependencyInjection;
|
using Volo.Abp.DependencyInjection;
|
||||||
using Volo.Abp.Security.Claims;
|
using Volo.Abp.Security.Claims;
|
||||||
|
using Yi.Framework.Core.Authentication;
|
||||||
using Yi.Framework.Rbac.Domain.Managers;
|
using Yi.Framework.Rbac.Domain.Managers;
|
||||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
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["access_token"] = access_Token;
|
||||||
context.Response.Headers["refresh_token"] = refresh_Token;
|
context.Response.Headers["refresh_token"] = refresh_Token;
|
||||||
|
|
||||||
|
|
||||||
//请求头替换,补充后续鉴权逻辑
|
//请求头替换,补充后续鉴权逻辑
|
||||||
context.Request.Headers["Authorization"] = "Bearer " + access_Token;
|
context.Request.Headers["Authorization"] = "Bearer " + access_Token;
|
||||||
|
|
||||||
|
//刷新鉴权状态
|
||||||
|
context.RefreshAuthentication();
|
||||||
}
|
}
|
||||||
//刷新token 与 access_token都失效了
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
//context.Response.StatusCode = StatusCodes.Status401Unauthorized;
|
|
||||||
//return;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user