feat: 添加 SignalR 基础设施(Hub + Redis Backplane + JWT 认证)
Some checks failed
Build and Deploy TenantApi + SkuWorker / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy TenantApi + SkuWorker / build-and-deploy (push) Has been cancelled
- 添加 Microsoft.AspNetCore.SignalR.StackExchangeRedis NuGet 包 - 新建 OrderBoardHub(连接/断开/切换门店 Group) - 新建 SignalRJwtEvents(WebSocket query string JWT 提取) - Program.cs 注册 SignalR + Redis Backplane + MapHub - JwtAuthenticationExtensions 添加 OnMessageReceived 事件 - CORS 修复 AllowAnyOrigin 与 AllowCredentials 互斥问题
This commit is contained in:
@@ -47,6 +47,24 @@ public static class JwtAuthenticationExtensions
|
||||
NameClaimType = ClaimTypes.NameIdentifier,
|
||||
RoleClaimType = ClaimTypes.Role
|
||||
};
|
||||
|
||||
// SignalR WebSocket 通过 query string 传递 JWT
|
||||
options.Events = new JwtBearerEvents
|
||||
{
|
||||
OnMessageReceived = context =>
|
||||
{
|
||||
if (context.Request.Path.StartsWithSegments("/hubs"))
|
||||
{
|
||||
var token = context.Request.Query["access_token"];
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
context.Token = token;
|
||||
}
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
|
||||
Reference in New Issue
Block a user