Dashboard - dotnet-shashlik/shashlik.eventbus GitHub Wiki

Dashboard

NuGet:Shashlik.EventBus.Dashboard

dashboard

配置

// 注册服务
services.AddEventBus()
    .AddRelationDb(options => options.UseConnection(DataType.MySql, "..."))
    .AddRabbitMQ(r => { /* ... */ })
    .AddDashboard(options =>
    {
        // 使用 Secret 认证(推荐)
        // secret 必须为 32 字符,仅支持英文/数字/常见密码特殊符号
        // !@#$%^&*()_+-=[]{};':"\|,.<>/?`~
        options.UseSecretAuthenticate("ShashlikEventBus.DashboardKey#32");

        // 或使用自定义认证类
        // options.UseAuthenticate<MyAuthenticate>();
    });

// 在 app.UseRouting() 之后
app.UseEventBusDashboard();

安全提示: 未配置 IEventBusDashboardAuthentication 时,Dashboard 默认拒绝所有请求并返回 503。必须配置认证才能访问 Dashboard,避免发布/重试接口被未授权访问。

配置项 EventBusDashboardOption

配置项 默认值 说明
UrlPrefix /eventBus Dashboard 路由前缀
AuthenticateProvider null 认证提供类,需实现 IEventBusDashboardAuthentication
AuthenticateSecret ShashlikEventBus.DashboardKey#32 SecretCookieAuthenticate 使用的明文 secret,长度必须 32 字符,仅允许英文/数字/常见密码特殊符号(!@#$%^&*()_+-=[]{};':"|,.<>/?~`),同时用作 Cookie 签名 key
AuthenticateSecretCookieName shashlik-eventbus-secret Cookie 名称
AuthenticateSecretCookieOptions 过期 2 小时 Cookie 配置函数

SecretCookieAuthenticate

内置的基于 Cookie + Secret 的认证方式:

  1. 进入 Dashboard 页面时,检查 Cookie 是否存在且签名有效
  2. 无效则跳转至 Secret 认证页面
  3. 用户输入 secret 后,服务端以常量时间比对校验明文 secret,校验通过后生成随机 token 并以 AuthenticateSecret 的 ASCII 字节作为 HMAC-SHA256 key 签名后写入 Cookie
  4. 后续请求通过 HMAC 签名校验 Cookie 有效性

Secret 以明文形式存储在 EventBusDashboardOption 中,同时承担登录凭证Cookie HMAC 签名 key两个角色。启动期 AddDashboard 会校验 AuthenticateSecret 的格式:长度必须为 32 字符,且只能包含英文、数字和指定常用密码特殊符号。修改 secret 会同时影响所有已签发 Cookie 的校验——旧 Cookie 立即失效。

自定义 Secret

.AddDashboard(options =>
{
    // 必须为 32 字符,符合字符集要求
    options.UseSecretAuthenticate("Your#Custom32BytesSecretKey_Abcd12");
})

如需调整 Cookie 行为(过期时间、SameSite 等):

.AddDashboard(options =>
{
    options.UseSecretAuthenticate(
        "Your#Custom32BytesSecretKey_Abcd12",
        authenticateSecretCookieName: null,
        authenticateSecretCookieOptions: _ => new CookieOptions
        {
            Expires = DateTimeOffset.Now.AddHours(2),
            SameSite = SameSiteMode.Strict,
            SecurePolicy = CookieSecurePolicy.Always
        });
})

自定义认证

实现 IEventBusDashboardAuthentication 接口:

public class MyAuthenticate : IEventBusDashboardAuthentication
{
    public Task<bool> AuthenticateAsync(HttpContext context)
    {
        // 自定义认证逻辑(如对接现有身份系统、JWT、SSO 等)
        return Task.FromResult(true);
    }
}

注册:

.AddDashboard(options => options.UseAuthenticate<MyAuthenticate>())
⚠️ **GitHub.com Fallback** ⚠️