All checks were successful
Build and Deploy / deploy (push) Successful in 4m34s
Co-authored-by: Cursor <cursoragent@cursor.com>
37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
namespace SimpleAdmin.Api.Auth;
|
||
|
||
/// <summary>
|
||
/// 仓配开放 API:Authorization 直接传 JWT(不要求 Bearer 前缀)。
|
||
/// 在进入 JwtBearer 前补上 Bearer,以便复用现有鉴权。
|
||
/// </summary>
|
||
public sealed class WarehouseOpenApiAuthHeaderMiddleware(RequestDelegate next)
|
||
{
|
||
public Task InvokeAsync(HttpContext context)
|
||
{
|
||
if (context.Request.Path.StartsWithSegments("/openapi/warehouse"))
|
||
{
|
||
NormalizeAuthorizationHeader(context.Request.Headers);
|
||
}
|
||
|
||
return next(context);
|
||
}
|
||
|
||
private static void NormalizeAuthorizationHeader(IHeaderDictionary headers)
|
||
{
|
||
var auth = headers.Authorization.ToString();
|
||
if (string.IsNullOrWhiteSpace(auth))
|
||
{
|
||
return;
|
||
}
|
||
|
||
auth = auth.Trim();
|
||
if (auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 无 scheme:Authorization: {token}
|
||
headers.Authorization = "Bearer " + auth;
|
||
}
|
||
}
|