ISimApiAuthChecker 改为按接口注册 + getAll<T>() 解析,移除 SimApiOptions.authCheckers 手动列表
This commit is contained in:
@@ -2,18 +2,18 @@ version = 0
|
|||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
soulsoft_extensions_options_configuration = {version = "1.0.20260528"}
|
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_configuration = {version = "1.0.20260528"}
|
||||||
soulsoft_extensions_injection = {version = "1.0.20260528"}
|
|
||||||
soulsoft_web_http = {version = "1.0.20260528"}
|
soulsoft_web_http = {version = "1.0.20260528"}
|
||||||
soulsoft_extensions_options = {version = "1.0.20260528"}
|
soulsoft_web_hosting = {version = "1.0.20260528"}
|
||||||
redis = {version = "1.0.20260627"}
|
|
||||||
soulsoft_serialization = {version = "1.0.20260528"}
|
soulsoft_serialization = {version = "1.0.20260528"}
|
||||||
soulsoft_web_routing = {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_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_web_cors = {version = "1.0.20260528"}
|
||||||
soulsoft_extensions_logging = {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"}
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import simapi.communications.*
|
|||||||
import simapi.configurations.*
|
import simapi.configurations.*
|
||||||
import simapi.controllers.*
|
import simapi.controllers.*
|
||||||
import simapi.helpers.*
|
import simapi.helpers.*
|
||||||
|
import simapi.interfaces.*
|
||||||
import simapi.logger.*
|
import simapi.logger.*
|
||||||
import simapi.middlewares.*
|
import simapi.middlewares.*
|
||||||
|
|
||||||
@@ -283,14 +284,13 @@ public class SimApiExtensions {
|
|||||||
builder.services.addSingleton<SimApiAuth, SimApiAuth>()
|
builder.services.addSingleton<SimApiAuth, SimApiAuth>()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ISimApiAuthChecker 自动扫描注册(对齐 C# AddSimApi 中遍历调用者程序集 AddScoped 注册 checker)
|
// ISimApiAuthChecker 自动扫描注册(对齐 C# AddSimApi 中遍历调用者程序集 AddScoped(ISimApiAuthChecker, type))
|
||||||
// 扫描调用者包中的 ISimApiAuthChecker 实现类:填充 options.authCheckers(供 @SimApiAuth 执行时解析)
|
// 扫描调用者包中的实现类,按【接口】注册;执行时用 getAll<ISimApiAuthChecker>() 一次解析全部实现,
|
||||||
// 并注册到 DI(按实现类类型注册,供 getOrThrow(checkerType) 解析)
|
// 无需在 SimApiOptions 里维护类型列表。
|
||||||
if (options.enableSimApiAuth) {
|
if (options.enableSimApiAuth) {
|
||||||
let checkers = SimApiControllerScanner.scanAuthCheckers()
|
let checkers = SimApiControllerScanner.scanAuthCheckers()
|
||||||
for (checkerType in checkers) {
|
for (checkerType in checkers) {
|
||||||
options.authCheckers.add(checkerType)
|
builder.services.addScoped(TypeInfo.of<ISimApiAuthChecker>(), checkerType)
|
||||||
builder.services.addScoped(checkerType)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,17 +7,11 @@
|
|||||||
package simapi.configurations
|
package simapi.configurations
|
||||||
|
|
||||||
import std.collection.*
|
import std.collection.*
|
||||||
import std.reflect.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SimApi 全局配置:对应 C# 的 SimApi.Configurations.SimApiOptions。
|
* SimApi 全局配置:对应 C# 的 SimApi.Configurations.SimApiOptions。
|
||||||
*/
|
*/
|
||||||
public class SimApiOptions {
|
public class SimApiOptions {
|
||||||
/**
|
|
||||||
* 已注册的 ISimApiAuthChecker 类型列表(由 addSimApi 扫描调用者程序集填充,
|
|
||||||
* 运行时在 @SimApiAuth 鉴权时逐个从 DI 解析执行,对齐 C# AddScoped 扫描)。
|
|
||||||
*/
|
|
||||||
public var authCheckers: ArrayList<TypeInfo> = ArrayList<TypeInfo>()
|
|
||||||
/**
|
/**
|
||||||
* Redis 配置;配置则使用 Redis,不配则自动使用 InMemory。
|
* Redis 配置;配置则使用 Redis,不配则自动使用 InMemory。
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -265,18 +265,15 @@ struct SimApiActionInvoker {
|
|||||||
SimApiError.error(code: 401, message: "需要登录")
|
SimApiError.error(code: 401, message: "需要登录")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 遍历执行 ISimApiAuthChecker(对齐 C#:先执行 checker,可修改 loginItem,再做类型判断)
|
// 2. 遍历执行 ISimApiAuthChecker(对齐 C# GetServices<ISimApiAuthChecker>():一次解析全部实现)
|
||||||
let token = match (context.items.get("LoginToken")) {
|
let token = match (context.items.get("LoginToken")) {
|
||||||
case Some(v) => if (let s: String <- v) { s } else { "" }
|
case Some(v) => if (let s: String <- v) { s } else { "" }
|
||||||
case None => ""
|
case None => ""
|
||||||
}
|
}
|
||||||
let options = context.services.getOrThrow<SimApiOptions>()
|
let checkers = context.services.getAll<ISimApiAuthChecker>()
|
||||||
for (checkerType in options.authCheckers) {
|
for (checker in checkers) {
|
||||||
let instance = context.services.getOrThrow(checkerType)
|
|
||||||
if (let checker: ISimApiAuthChecker <- instance) {
|
|
||||||
checker.run(loginItem, token)
|
checker.run(loginItem, token)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 类型权限校验 → 403(对齐 C# Types.Intersect(loginInfo.Type).Any(),支持逗号分隔多类型)
|
// 3. 类型权限校验 → 403(对齐 C# Types.Intersect(loginInfo.Type).Any(),支持逗号分隔多类型)
|
||||||
if (!auth.`type`.isEmpty()) {
|
if (!auth.`type`.isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user