2026-06-02 17:58:10 +08:00
|
|
|
using Microsoft.AspNetCore.Authentication;
|
2026-06-01 13:42:40 +08:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2026-06-02 17:58:10 +08:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2026-06-01 13:42:40 +08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2026-06-02 17:58:10 +08:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2026-06-01 13:42:40 +08:00
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
|
|
|
|
|
using QYZH.InteractiveMagazine.Models.Settings;
|
|
|
|
|
using System.Text;
|
2026-06-02 17:58:10 +08:00
|
|
|
using System.Text.Encodings.Web;
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
|
|
|
namespace QYZH.InteractiveMagazine.Infrastructure.Extensions;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 统一服务注册扩展
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class DependencyInjectionExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 注册基础设施服务
|
|
|
|
|
/// </summary>
|
2026-06-02 17:58:10 +08:00
|
|
|
/// <param name="services">服务集合</param>
|
2026-06-01 13:42:40 +08:00
|
|
|
/// <param name="configuration">配置</param>
|
2026-06-02 17:58:10 +08:00
|
|
|
/// <param name="environment">运行环境</param>
|
|
|
|
|
public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
|
2026-06-01 13:42:40 +08:00
|
|
|
{
|
2026-06-02 17:58:10 +08:00
|
|
|
AddJwtAuthentication(services, configuration, environment);
|
2026-06-01 13:42:40 +08:00
|
|
|
|
|
|
|
|
services.AddTransient<GlobalExceptionMiddleware>();
|
|
|
|
|
services.AddTransient<OperationLogMiddleware>();
|
2026-06-01 17:59:23 +08:00
|
|
|
|
2026-06-01 13:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 配置JWT认证
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="services">服务集合</param>
|
|
|
|
|
/// <param name="configuration">配置</param>
|
2026-06-02 17:58:10 +08:00
|
|
|
/// <param name="environment">运行环境</param>
|
|
|
|
|
private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
|
2026-06-01 13:42:40 +08:00
|
|
|
{
|
2026-06-02 17:58:10 +08:00
|
|
|
// 开发环境下跳过 JWT 验证
|
|
|
|
|
if (environment?.IsDevelopment() == true)
|
|
|
|
|
{
|
|
|
|
|
services.AddAuthentication("NoAuth")
|
|
|
|
|
.AddScheme<AuthenticationSchemeOptions, NoAuthHandler>("NoAuth", options => { });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 13:42:40 +08:00
|
|
|
var jwtSettings = configuration.GetSection("JwtSettings").Get<JwtSettings>()!;
|
|
|
|
|
|
|
|
|
|
services.AddSingleton(jwtSettings);
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidIssuer = jwtSettings.Issuer,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidAudience = jwtSettings.Audience,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey!)),
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ClockSkew = TimeSpan.Zero
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-02 17:58:10 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 开发环境免认证处理器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class NoAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
|
|
|
|
{
|
|
|
|
|
public NoAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
|
|
|
|
|
: base(options, logger, encoder, clock)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
{
|
|
|
|
|
// 开发环境下始终认证成功
|
|
|
|
|
var claims = new[]
|
|
|
|
|
{
|
|
|
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Name, "DevUser"),
|
|
|
|
|
new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.NameIdentifier, "0")
|
|
|
|
|
};
|
|
|
|
|
var identity = new System.Security.Claims.ClaimsIdentity(claims, Scheme.Name);
|
|
|
|
|
var principal = new System.Security.Claims.ClaimsPrincipal(identity);
|
|
|
|
|
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(AuthenticateResult.Success(ticket));
|
|
|
|
|
}
|
|
|
|
|
}
|