From a04ab2f400b390936e963dca7b09bf13e1d0699a Mon Sep 17 00:00:00 2001 From: xRain Date: Sun, 16 Aug 2026 22:46:57 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20addSimApi/useSimApi=20=E6=8E=A5?= =?UTF-8?q?=E5=85=A5=E5=86=85=E7=BD=AE=E8=B7=AF=E7=94=B1/AuthSDK/checker?= =?UTF-8?q?=20=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 内置路由:RouteOptions 自定义路径用 mapGet/mapPost 真实注册(默认值由特性路由覆盖) - AuthGate:enableSimApiAuthGate 时注册 AuthClient/Center/Iam 单例并挂载网关中间件 - ISimApiAuthChecker 自动扫描:enableSimApiAuth 时扫描调用者包实现类填充 authCheckers + AddScoped --- src/extensions/SimApiExtensions.cj | 78 ++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 5 deletions(-) diff --git a/src/extensions/SimApiExtensions.cj b/src/extensions/SimApiExtensions.cj index 6989dce..e75cbd5 100644 --- a/src/extensions/SimApiExtensions.cj +++ b/src/extensions/SimApiExtensions.cj @@ -20,6 +20,7 @@ import soulsoft_web_cors.* import soulsoft_web_routing.* import soulsoft_extensions_injection.* import soulsoft_extensions_logging.* +import simapi.authsdk.* import simapi.communications.* import simapi.configurations.* import simapi.controllers.* @@ -114,6 +115,17 @@ private func addSimApiCore(builder: WebHostBuilder, options: SimApiOptions): Web builder.services.addSingleton() } + // ISimApiAuthChecker 自动扫描注册(对齐 C# AddSimApi 中遍历调用者程序集 AddScoped 注册 checker) + // 扫描调用者包中的 ISimApiAuthChecker 实现类:填充 options.authCheckers(供 @SimApiAuth 执行时解析) + // 并注册到 DI(按实现类类型注册,供 getOrThrow(checkerType) 解析) + if (options.enableSimApiAuth) { + let checkers = SimApiControllerScanner.scanAuthCheckers() + for (checkerType in checkers) { + options.authCheckers.add(checkerType) + builder.services.addScoped(checkerType) + } + } + // 缓存(DI 自动注入 SimApiOptions) if (options.enableSimApiCache) { builder.services.addSingleton() @@ -124,6 +136,13 @@ private func addSimApiCore(builder: WebHostBuilder, options: SimApiOptions): Web builder.services.addSingleton() } + // AuthGate 认证中心 SDK(对齐 C# 注册 SimApiAuthClient/Center/Iam 单例) + if (options.enableSimApiAuthGate) { + builder.services.addSingleton() + builder.services.addSingleton() + builder.services.addSingleton() + } + // CORS(对齐 C# builder.Services.AddCors(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())) if (options.enableCors) { builder.services.addCors { @@ -206,9 +225,10 @@ extend WebHost <: SimApiHostExtensions { this.useCors() } - // AuthGate(占位,对齐 C# UseMiddleware) + // AuthGate(对齐 C# UseMiddleware) if (options.enableSimApiAuthGate) { logger.info("开始配置 SimApiAuthGate...") + this.use() } // 认证中间件(对齐 C# builder.UseMiddleware()) @@ -229,14 +249,62 @@ extend WebHost <: SimApiHostExtensions { this.use() } - // 内置路由 - if (let Some(route) <- options.simApiRouteOptions.userInfoRoute) { + // 内置路由:RouteOptions 自定义路径时真实注册(默认路径已由内置控制器特性路由覆盖, + // 对齐 C# MapControllerRoute 语义;soulsoft 无约定路由 defaults,用 mapGet/mapPost 委托实现) + let routeOptions = options.simApiRouteOptions + if (let Some(route) <- routeOptions.userInfoRoute) { + if (route != "/user/info") { + this.mapPost(route, { context => + let controller = ActivatorUtilities.createInstance(context.services, + TypeInfo.of()) + if (let c: SimApiBaseController <- controller) { + c.bindRequestContext(context) + } + if (let c: SimApiCommonController <- controller) { + SimApiResultWriter.write(context, c.userInfo()) + } + }) + } logger.info("注册内置Route: UserInfo => ${route}") } - if (let Some(route) <- options.simApiRouteOptions.logoutRoute) { + if (let Some(route) <- routeOptions.logoutRoute) { + if (route != "/auth/logout") { + this.mapPost(route, { context => + let controller = ActivatorUtilities.createInstance(context.services, + TypeInfo.of()) + if (let c: SimApiBaseController <- controller) { + c.bindRequestContext(context) + } + if (let c: SimApiAuthController <- controller) { + SimApiResultWriter.write(context, c.logout()) + } + }) + } logger.info("注册内置Route: Logout => ${route}") } - if (let Some(route) <- options.simApiRouteOptions.webConfigRoute) { + if (let Some(route) <- routeOptions.webConfigRoute) { + if (route != "/config") { + this.mapGet(route, { context => + let controller = ActivatorUtilities.createInstance(context.services, + TypeInfo.of()) + if (let c: SimApiBaseController <- controller) { + c.bindRequestContext(context) + } + if (let c: SimApiCommonController <- controller) { + SimApiResultWriter.write(context, c.webConfig()) + } + }) + this.mapPost(route, { context => + let controller = ActivatorUtilities.createInstance(context.services, + TypeInfo.of()) + if (let c: SimApiBaseController <- controller) { + c.bindRequestContext(context) + } + if (let c: SimApiCommonController <- controller) { + SimApiResultWriter.write(context, c.webConfigPost()) + } + }) + } logger.info("注册内置Route: WebConfig => ${route}") }