chore: 提交现有修改
This commit is contained in:
@@ -11,54 +11,43 @@ namespace TakeoutSaaS.Module.Tenancy;
|
||||
/// <summary>
|
||||
/// 多租户解析中间件:支持 Header、域名与 Token Claim 的优先级解析。
|
||||
/// </summary>
|
||||
public sealed class TenantResolutionMiddleware
|
||||
/// <remarks>
|
||||
/// 初始化中间件。
|
||||
/// </remarks>
|
||||
public sealed class TenantResolutionMiddleware(
|
||||
RequestDelegate next,
|
||||
ILogger<TenantResolutionMiddleware> logger,
|
||||
ITenantContextAccessor tenantContextAccessor,
|
||||
IOptionsMonitor<TenantResolutionOptions> optionsMonitor)
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<TenantResolutionMiddleware> _logger;
|
||||
private readonly ITenantContextAccessor _tenantContextAccessor;
|
||||
private readonly IOptionsMonitor<TenantResolutionOptions> _optionsMonitor;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化中间件。
|
||||
/// </summary>
|
||||
public TenantResolutionMiddleware(
|
||||
RequestDelegate next,
|
||||
ILogger<TenantResolutionMiddleware> logger,
|
||||
ITenantContextAccessor tenantContextAccessor,
|
||||
IOptionsMonitor<TenantResolutionOptions> optionsMonitor)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
_tenantContextAccessor = tenantContextAccessor;
|
||||
_optionsMonitor = optionsMonitor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析租户并将上下文注入请求。
|
||||
/// </summary>
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var options = _optionsMonitor.CurrentValue ?? new TenantResolutionOptions();
|
||||
var options = optionsMonitor.CurrentValue ?? new TenantResolutionOptions();
|
||||
if (ShouldSkip(context.Request.Path, options))
|
||||
{
|
||||
await _next(context);
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var tenantContext = ResolveTenant(context, options);
|
||||
_tenantContextAccessor.Current = tenantContext;
|
||||
tenantContextAccessor.Current = tenantContext;
|
||||
context.Items[TenantConstants.HttpContextItemKey] = tenantContext;
|
||||
|
||||
if (!tenantContext.IsResolved)
|
||||
{
|
||||
_logger.LogDebug("未能解析租户:{Path}", context.Request.Path);
|
||||
logger.LogDebug("未能解析租户:{Path}", context.Request.Path);
|
||||
|
||||
if (options.ThrowIfUnresolved)
|
||||
{
|
||||
var response = ApiResponse.Error(ErrorCodes.BadRequest, "缺少租户标识");
|
||||
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
await context.Response.WriteAsJsonAsync(response, cancellationToken: context.RequestAborted);
|
||||
_tenantContextAccessor.Current = null;
|
||||
tenantContextAccessor.Current = null;
|
||||
context.Items.Remove(TenantConstants.HttpContextItemKey);
|
||||
return;
|
||||
}
|
||||
@@ -66,11 +55,11 @@ public sealed class TenantResolutionMiddleware
|
||||
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
await next(context);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_tenantContextAccessor.Current = null;
|
||||
tenantContextAccessor.Current = null;
|
||||
context.Items.Remove(TenantConstants.HttpContextItemKey);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user