From f88eee8d64897460d7514d060ee9d1c193616ed2 Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 18 Aug 2026 03:19:27 +0800 Subject: [PATCH] =?UTF-8?q?ISimApiAuthChecker=20=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8C=89=E6=8E=A5=E5=8F=A3=E6=B3=A8=E5=86=8C=20+=20getAll()?= =?UTF-8?q?=20=E8=A7=A3=E6=9E=90,=E7=A7=BB=E9=99=A4=20SimApiOptions.authCh?= =?UTF-8?q?eckers=20=E6=89=8B=E5=8A=A8=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cjpm.lock | 16 ++++++++-------- src/SimApiExtensions.cj | 10 +++++----- src/configurations/SimApiOptions.cj | 6 ------ src/helpers/SimApiRequestDelegateFactory.cj | 11 ++++------- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/cjpm.lock b/cjpm.lock index db5ce18..c7fe3c5 100644 --- a/cjpm.lock +++ b/cjpm.lock @@ -2,18 +2,18 @@ version = 0 [requires] soulsoft_extensions_options_configuration = {version = "1.0.20260528"} + soulsoft_extensions_logging_console = {version = "1.0.20260528"} + soulsoft_extensions_logging_configuration = {version = "1.0.20260528"} soulsoft_extensions_configuration = {version = "1.0.20260528"} - soulsoft_extensions_injection = {version = "1.0.20260528"} soulsoft_web_http = {version = "1.0.20260528"} - soulsoft_extensions_options = {version = "1.0.20260528"} - redis = {version = "1.0.20260627"} + soulsoft_web_hosting = {version = "1.0.20260528"} soulsoft_serialization = {version = "1.0.20260528"} soulsoft_web_routing = {version = "1.0.20260528"} - soulsoft_web_hosting = {version = "1.0.20260528"} soulsoft_web_mvc = {version = "1.0.20260528"} + soulsoft_identity_claims = {version = "1.0.20260528"} + soulsoft_extensions_injection = {version = "1.0.20260528"} + soulsoft_extensions_hosting = {version = "1.0.20260528"} + soulsoft_extensions_options = {version = "1.0.20260528"} + redis = {version = "1.0.20260627"} soulsoft_web_cors = {version = "1.0.20260528"} soulsoft_extensions_logging = {version = "1.0.20260528"} - soulsoft_identity_claims = {version = "1.0.20260528"} - soulsoft_extensions_logging_configuration = {version = "1.0.20260528"} - soulsoft_extensions_logging_console = {version = "1.0.20260528"} - soulsoft_extensions_hosting = {version = "1.0.20260528"} diff --git a/src/SimApiExtensions.cj b/src/SimApiExtensions.cj index f4c6c32..f497cbb 100644 --- a/src/SimApiExtensions.cj +++ b/src/SimApiExtensions.cj @@ -39,6 +39,7 @@ import simapi.communications.* import simapi.configurations.* import simapi.controllers.* import simapi.helpers.* +import simapi.interfaces.* import simapi.logger.* import simapi.middlewares.* @@ -283,14 +284,13 @@ public class SimApiExtensions { builder.services.addSingleton() } - // ISimApiAuthChecker 自动扫描注册(对齐 C# AddSimApi 中遍历调用者程序集 AddScoped 注册 checker) - // 扫描调用者包中的 ISimApiAuthChecker 实现类:填充 options.authCheckers(供 @SimApiAuth 执行时解析) - // 并注册到 DI(按实现类类型注册,供 getOrThrow(checkerType) 解析) + // ISimApiAuthChecker 自动扫描注册(对齐 C# AddSimApi 中遍历调用者程序集 AddScoped(ISimApiAuthChecker, type)) + // 扫描调用者包中的实现类,按【接口】注册;执行时用 getAll() 一次解析全部实现, + // 无需在 SimApiOptions 里维护类型列表。 if (options.enableSimApiAuth) { let checkers = SimApiControllerScanner.scanAuthCheckers() for (checkerType in checkers) { - options.authCheckers.add(checkerType) - builder.services.addScoped(checkerType) + builder.services.addScoped(TypeInfo.of(), checkerType) } } diff --git a/src/configurations/SimApiOptions.cj b/src/configurations/SimApiOptions.cj index 57e67c9..e906469 100644 --- a/src/configurations/SimApiOptions.cj +++ b/src/configurations/SimApiOptions.cj @@ -7,17 +7,11 @@ package simapi.configurations import std.collection.* -import std.reflect.* /** * SimApi 全局配置:对应 C# 的 SimApi.Configurations.SimApiOptions。 */ public class SimApiOptions { - /** - * 已注册的 ISimApiAuthChecker 类型列表(由 addSimApi 扫描调用者程序集填充, - * 运行时在 @SimApiAuth 鉴权时逐个从 DI 解析执行,对齐 C# AddScoped 扫描)。 - */ - public var authCheckers: ArrayList = ArrayList() /** * Redis 配置;配置则使用 Redis,不配则自动使用 InMemory。 */ diff --git a/src/helpers/SimApiRequestDelegateFactory.cj b/src/helpers/SimApiRequestDelegateFactory.cj index 77b2742..5366e6b 100644 --- a/src/helpers/SimApiRequestDelegateFactory.cj +++ b/src/helpers/SimApiRequestDelegateFactory.cj @@ -265,17 +265,14 @@ struct SimApiActionInvoker { SimApiError.error(code: 401, message: "需要登录") } - // 2. 遍历执行 ISimApiAuthChecker(对齐 C#:先执行 checker,可修改 loginItem,再做类型判断) + // 2. 遍历执行 ISimApiAuthChecker(对齐 C# GetServices():一次解析全部实现) let token = match (context.items.get("LoginToken")) { case Some(v) => if (let s: String <- v) { s } else { "" } case None => "" } - let options = context.services.getOrThrow() - for (checkerType in options.authCheckers) { - let instance = context.services.getOrThrow(checkerType) - if (let checker: ISimApiAuthChecker <- instance) { - checker.run(loginItem, token) - } + let checkers = context.services.getAll() + for (checker in checkers) { + checker.run(loginItem, token) } // 3. 类型权限校验 → 403(对齐 C# Types.Intersect(loginInfo.Type).Any(),支持逗号分隔多类型)