feat: addSimApi/useSimApi 接入内置路由/AuthSDK/checker 扫描

- 内置路由:RouteOptions 自定义路径用 mapGet/mapPost 真实注册(默认值由特性路由覆盖)
- AuthGate:enableSimApiAuthGate 时注册 AuthClient/Center/Iam 单例并挂载网关中间件
- ISimApiAuthChecker 自动扫描:enableSimApiAuth 时扫描调用者包实现类填充 authCheckers + AddScoped
This commit is contained in:
2026-08-16 22:46:57 +08:00
parent c7b8148ff5
commit a04ab2f400
+73 -5
View File
@@ -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<SimApiAuth, SimApiAuth>()
}
// 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<SimApiCache, SimApiCache>()
@@ -124,6 +136,13 @@ private func addSimApiCore(builder: WebHostBuilder, options: SimApiOptions): Web
builder.services.addSingleton<SimApiHttpClient, SimApiHttpClient>()
}
// AuthGate 认证中心 SDK(对齐 C# 注册 SimApiAuthClient/Center/Iam 单例)
if (options.enableSimApiAuthGate) {
builder.services.addSingleton<SimApiAuthClient, SimApiAuthClient>()
builder.services.addSingleton<SimApiAuthCenter, SimApiAuthCenter>()
builder.services.addSingleton<SimApiAuthIam, SimApiAuthIam>()
}
// 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<SimApiAuthCenterMiddleware>
// AuthGate(对齐 C# UseMiddleware<SimApiAuthCenterMiddleware>
if (options.enableSimApiAuthGate) {
logger.info("开始配置 SimApiAuthGate...")
this.use<SimApiAuthCenterMiddleware>()
}
// 认证中间件(对齐 C# builder.UseMiddleware<SimApiAuthMiddleware>()
@@ -229,14 +249,62 @@ extend WebHost <: SimApiHostExtensions {
this.use<SimApiExceptionMiddleware>()
}
// 内置路由
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<SimApiCommonController>())
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<SimApiAuthController>())
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<SimApiCommonController>())
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<SimApiCommonController>())
if (let c: SimApiBaseController <- controller) {
c.bindRequestContext(context)
}
if (let c: SimApiCommonController <- controller) {
SimApiResultWriter.write(context, c.webConfigPost())
}
})
}
logger.info("注册内置Route: WebConfig => ${route}")
}