Compare commits

..
3 Commits
Author SHA1 Message Date
xrain b534ba9818 gitignore 忽略 .openbitfun 目录 2026-10-06 23:12:52 +08:00
xrain 8d7ba36fb4 新增 ISimApiTokenResolver 自定义Token解析扩展
GetLogin 优先按注册顺序执行 TokenResolver 链, 非null即短路, 全部为null才回退内置缓存; resolver 每次调用在独立作用域内解析并即时释放
2026-10-06 23:12:48 +08:00
xrain e4f051cb61 递归字符串处理 2026-09-04 04:32:52 +08:00
5 changed files with 124 additions and 18 deletions
+2
View File
@@ -438,3 +438,5 @@ MigrationBackup/
# BitFun snapshot data - auto managed
.bitfun/
/.bitfun/search/flashgrep-index/
.openbitfun/
/.openbitfun/search/flashgrep-index/
+12 -1
View File
@@ -1,9 +1,11 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using SimApi.Communications;
using SimApi.Interfaces;
using StackExchange.Redis;
namespace SimApi.Helpers;
@@ -18,14 +20,16 @@ public class SimApiAuth
private readonly IDistributedCache _cache;
private readonly IDatabase? _redisDb;
private readonly IServiceScopeFactory _scopeFactory;
// InMemory 模式:用户 → Token集合
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, byte>> _userTokens = new();
public SimApiAuth(IDistributedCache cache, IServiceProvider sp)
public SimApiAuth(IDistributedCache cache, IServiceProvider sp, IServiceScopeFactory scopeFactory)
{
_cache = cache;
_redisDb = sp.GetService<IConnectionMultiplexer>()?.GetDatabase();
_scopeFactory = scopeFactory;
}
/// <summary>
@@ -88,6 +92,13 @@ public class SimApiAuth
/// <returns></returns>
public SimApiLoginItem? GetLogin(string token)
{
using var scope = _scopeFactory.CreateScope(); // 用完即释放
foreach (var resolver in scope.ServiceProvider.GetServices<ISimApiTokenResolver>())
{
var item = resolver.Run(token);
if (item != null) return item;
}
var cacheKey = TokenCacheKey.Replace("{token}", token);
var login = _cache.GetString(cacheKey);
var resp = login != null ? SimApiUtil.FromJson<SimApiLoginItem>(login) : null;
+31
View File
@@ -0,0 +1,31 @@
using SimApi.Communications;
namespace SimApi.Interfaces;
/// <summary>
/// 自定义 Token 解析扩展点,用于让 <c>SimApiAuth.GetLogin</c> 识别本库之外签发的 Token(JWT、第三方登录态等)。
/// </summary>
/// <remarks>
/// <b>注册</b>:本库不会自动发现实现,必须由使用者自行注册。<c>AddScoped</c> / <c>AddSingleton</c> / <c>AddTransient</c>
/// 均可,因为每次调用都在独立作用域内解析。仅在 <c>EnableSimApiAuth = true</c> 时才会被执行。<br/>
/// <b>调用时机</b>:每次 <c>GetLogin</c>(即每个携带 Token 的请求)都会先执行全部 resolver,再回退到内置缓存查询;
/// 因此本机 <c>Login</c> 签发的 Token 同样会走一遍 resolver 链。<c>Logout</c> 与 <c>GetAllLogins</c> 内部也调用
/// <c>GetLogin</c>,同样会触发 resolver 链,其中 <c>GetAllLogins</c> 会按该用户的 Token 数量执行多次。<br/>
/// <b>顺序</b>:按注册顺序依次执行,返回非 <c>null</c> 即作为认证结果并立即短路,后续 resolver 与内置缓存不再参与。<br/>
/// <b>生命周期</b>:每次调用在根容器新建的子作用域内解析,因此可以安全注入 Scoped 服务(如 DbContext)。
/// 但该作用域不是当前请求的作用域,请求级事务 / UnitOfWork 的未提交改动不可见,实现应当只读。<br/>
/// <b>返回值</b>:必须是完全物化的对象。作用域在返回后立即释放,不可返回依赖延迟加载的 EF 追踪实体。<br/>
/// <b>Token 管理</b>:返回的登录态不受本库管理,<c>Login</c> / <c>Update</c> / <c>GetAllLogins</c> 不会包含它,
/// <c>Logout</c> 对它无效,吊销需由实现方自行处理。<br/>
/// <b>异常</b>:抛出的异常会直接冒泡并中断整条链(该请求认证失败)。表示“不是我的 Token”请返回 <c>null</c>,不要抛异常。<br/>
/// <b>性能</b>:这是认证热路径上的同步方法,查库等 I/O 会阻塞调用线程,建议在实现内部自行做短 TTL 缓存。
/// </remarks>
public interface ISimApiTokenResolver
{
/// <summary>
/// 尝试解析 Token。
/// </summary>
/// <param name="token">请求携带的 Token 原文。</param>
/// <returns>解析成功返回登录信息;不属于本实现的 Token 返回 <c>null</c> 由后续 resolver 继续处理。</returns>
public SimApiLoginItem? Run(string token);
}
+51 -16
View File
@@ -6,6 +6,7 @@ using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
@@ -119,8 +120,8 @@ public class SimApiRequestLogMiddleware(
}
/// <summary>
/// 与请求体相同的字段级裁剪:
/// JSON body 则把长度超过 maxLength 的字符串字段截断为前 maxLength 字符并追加 ...(原长度);
/// 与请求体相同的字段级裁剪(递归):
/// 遍历 JSON 对象/数组的所有层级, 把长度超过 maxLength 的字符串字段截断为前 maxLength 字符并追加 ...(原长度);
/// 空 body 或非 JSON body(表单/文件上传等)原样返回, 不裁剪
/// </summary>
private static string TrimJsonStringFields(string text, int maxLength)
@@ -130,32 +131,66 @@ public class SimApiRequestLogMiddleware(
return text;
}
Dictionary<string, object>? logs;
JsonNode? root;
try
{
logs = SimApiUtil.FromJson<Dictionary<string, object>>(text);
root = JsonNode.Parse(text);
}
catch (JsonException)
{
return text;
}
if (logs == null)
if (root == null)
{
return text;
}
// 遍历键快照后再赋值, 避免枚举过程中修改 Dictionary 引发异常
foreach (var key in logs.Keys.ToList())
{
if (logs[key] is not JsonElement { ValueKind: JsonValueKind.String } je) continue;
var str = je.GetString();
if (str?.Length > maxLength)
{
logs[key] = str[..maxLength] + $"...({str.Length})";
}
}
TrimNode(root, maxLength);
return SimApiUtil.Json(root);
}
return SimApiUtil.Json(logs);
/// <summary>
/// 递归裁剪节点下所有层级的字符串值:
/// 纯字符串字面量超长时截断, 数字/布尔/嵌套对象/数组继续向下递归
/// </summary>
private static void TrimNode(JsonNode? node, int maxLength)
{
switch (node)
{
case JsonObject obj:
// 键快照遍历后再赋值, 避免枚举过程中修改集合引发异常
foreach (var prop in obj.ToList())
{
if (prop.Value is JsonValue value &&
value.TryGetValue<string>(out var str) &&
str.Length > maxLength)
{
obj[prop.Key] = JsonValue.Create(str[..maxLength] + $"...({str.Length})");
}
else
{
TrimNode(prop.Value, maxLength);
}
}
break;
case JsonArray arr:
for (var i = 0; i < arr.Count; i++)
{
if (arr[i] is JsonValue value &&
value.TryGetValue<string>(out var str) &&
str.Length > maxLength)
{
arr[i] = JsonValue.Create(str[..maxLength] + $"...({str.Length})");
}
else
{
TrimNode(arr[i], maxLength);
}
}
break;
}
}
}
+28 -1
View File
@@ -78,7 +78,7 @@ SimApi/
├── Configurations/ # SimApiOptions + 各模块 Option 类
├── Controllers/ # SimApiBaseController, SimApiCommonController, SimApiAuthController
├── Helpers/ # SimApiError, SimApiAuth, SimApiCache, SimApiHttpClient, SimApiStorage, SimApiUtil, SimApiAesUtil
├── Interfaces/ # ISimApiAuthChecker
├── Interfaces/ # ISimApiAuthChecker, ISimApiTokenResolver
├── Middlewares/ # SimApiExceptionMiddleware, SimApiAuthMiddleware
├── Synapse/ # MQTT Pub/Sub + RPC + Config Store
├── Exceptions/ # SimApiException
@@ -221,6 +221,33 @@ public interface ISimApiAuthChecker {
// 实现后自动注册为 Scoped,每次认证后调用
```
### 自定义 Token 解析 — ISimApiTokenResolver
让认证识别**本库之外签发的 Token**(JWT、第三方登录态等),是 `GetLogin` 的前置扩展点。
```csharp
public interface ISimApiTokenResolver {
SimApiLoginItem? Run(string token); // 不是自己负责的 Token 返回 null
}
```
**必须由使用者手动注册**,本库不会自动发现实现(`AddScoped` / `AddSingleton` / `AddTransient` 均可):
```csharp
builder.Services.AddScoped<ISimApiTokenResolver, MyJwtTokenResolver>();
```
调用行为:
- 每次 `GetLogin`(即每个携带 Token 的请求)都会**先**按注册顺序执行全部 resolver,返回非 `null` 即短路生效;全部返回 `null` 才回退到内置缓存查询
- 因此本机 `Login()` 签发的 Token 同样会走一遍 resolver 链,实现应保持廉价,或自行做短 TTL 缓存
- `Logout` 与 `GetAllLogins` 内部也会调用 `GetLogin`,同样会触发 resolver 链,其中 `GetAllLogins` 会按该用户的 Token 数量执行多次
- 每次调用在新建的子作用域内解析,可以注入 Scoped 服务(如 `DbContext`);但它**不是当前请求的作用域**,请求级事务 / UnitOfWork 的未提交改动不可见,实现应当只读
- 返回值必须是完全物化的对象,作用域在返回后立即释放,不可返回依赖延迟加载的 EF 追踪实体
- 返回的登录态**不受本库管理**:`Login` / `Update` / `GetAllLogins` 不会包含它,`Logout` 对它无效,吊销需由实现方自行处理
- 抛出的异常会中断整条链并导致该请求认证失败;表示"不是我的 Token"请返回 `null`,不要抛异常
- 仅在 `EnableSimApiAuth = true` 时才会被执行
---
## 4. AuthSDK — Auth Center 网关 & IAM