diff --git a/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs b/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
index 9a97769..19f6a2e 100644
--- a/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
+++ b/QYZH.InteractiveMagazine.Infrastructure/Extensions/DependencyInjectionExtensions.cs
@@ -1,10 +1,16 @@
+using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
+using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using QYZH.InteractiveMagazine.Infrastructure.Middleware;
using QYZH.InteractiveMagazine.Models.Settings;
using System.Text;
+using System.Text.Encodings.Web;
namespace QYZH.InteractiveMagazine.Infrastructure.Extensions;
@@ -16,11 +22,12 @@ public static class DependencyInjectionExtensions
///
/// 注册基础设施服务
///
- /// 服务集合
+ /// 服务集合
/// 配置
- public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration)
+ /// 运行环境
+ public static void AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
{
- AddJwtAuthentication(services, configuration);
+ AddJwtAuthentication(services, configuration, environment);
services.AddTransient();
services.AddTransient();
@@ -32,8 +39,17 @@ public static class DependencyInjectionExtensions
///
/// 服务集合
/// 配置
- private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration)
+ /// 运行环境
+ private static void AddJwtAuthentication(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment? environment = null)
{
+ // 开发环境下跳过 JWT 验证
+ if (environment?.IsDevelopment() == true)
+ {
+ services.AddAuthentication("NoAuth")
+ .AddScheme("NoAuth", options => { });
+ return;
+ }
+
var jwtSettings = configuration.GetSection("JwtSettings").Get()!;
services.AddSingleton(jwtSettings);
@@ -55,3 +71,29 @@ public static class DependencyInjectionExtensions
});
}
}
+
+///
+/// 开发环境免认证处理器
+///
+public class NoAuthHandler : AuthenticationHandler
+{
+ public NoAuthHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
+ : base(options, logger, encoder, clock)
+ {
+ }
+
+ protected override Task 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));
+ }
+}
diff --git a/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs b/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
index 4f0bef2..39b8601 100644
--- a/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
+++ b/QYZH.InteractiveMagazine.Infrastructure/Middleware/GlobalExceptionMiddleware.cs
@@ -55,7 +55,7 @@ public class GlobalExceptionMiddleware : IMiddleware
context.Response.ContentType = "application/json";
context.Response.StatusCode = StatusCodes.Status400BadRequest;
- var response = BaseResponse