跳转到内容

流鉴权

Monibuca v6 支持两种流鉴权模式:

  1. 内置签名鉴权(v5 兼容)
  2. 自定义鉴权处理器(优先级更高)

鉴权按协议入口执行,当前已覆盖:

  • 推流:rtmpwebrtc(whip)webtransportsrtrtsp(record/announce)
  • 拉流:rtmpwebrtc(whep)flv/http-flv/ws-flvhlswebtransportsrtrtsp(describe/play)
global:
enableauth: true
publish:
key: "your-publish-key"
secretargname: secret
expireargname: expire
subscribe:
key: "your-subscribe-key"

说明:

  • enableauth=true 时开启鉴权逻辑
  • 推流使用 publish.key
  • 拉流使用 subscribe.key
  • 参数名可通过 secretargname / expireargname 覆盖,默认分别是 secret / expire

签名计算:

secret = md5(key + streamPath + expireHex)

其中:

  • keypublish.keysubscribe.key
  • streamPath:不含 query 的流路径(如 live/test
  • expireHex:16 进制 Unix 时间戳(秒)

校验规则:

  • expire 必须可解析为 16 进制时间戳,且未过期
  • secret 长度必须为 32
  • secret 与服务端计算值(忽略大小写)一致

live/test 为例:

rtmp://host/live/test?secret=...&expire=...
http://host:8180/flv/live/test.flv?secret=...&expire=...
http://host:8180/hls/live/test/index.m3u8?secret=...&expire=...
http://host:8180/webrtc/push/live/test?secret=...&expire=...
http://host:8180/webrtc/play/live/test?secret=...&expire=...
srt://host:6000?streamid=publish:/live/test?secret=...&expire=...

当你希望接入外部用户系统、权限中心或一次性票据时,可注册自定义鉴权处理器。

行为优先级:

  1. 自定义处理器(如果已注册)
  2. 内置 secret+expire 鉴权

也就是说,自定义处理器一旦返回结果,将作为最终鉴权结果。

StreamManagerApi 提供:

  • set_stream_auth_handler(handler)

处理器签名:

  • 输入:StreamAuthRequest
    • plugin_name
    • stream_path
    • query_string
    • params(已解析 query map)
    • is_publish
  • 返回:Result<()>
    • Ok(()):鉴权通过
    • Err(...):鉴权失败
manager.set_stream_auth_handler(Some(Arc::new(|req| {
if req.plugin_name == "rtmp" && req.is_publish {
let token = req.params.get("token").cloned().unwrap_or_default();
if token == "allow" {
return Ok(());
}
return Err(sdk::MonibucaError::InvalidInput("auth failed".into()));
}
Ok(())
})));

Admin 的“推流地址”弹窗支持按 v5 规则自动生成:

  • expire(16 进制时间戳)
  • secretmd5(key + streamPath + expire)

生成后会自动拼接到地址 query 中。

服务端提供签名生成接口:

GET /api/secret/{publish|subscribe}/{streamPath...}?expire=<hex>&plugin=<pluginName>

参数说明:

  • typepublishsubscribe
  • streamPath:流路径(支持 URL 编码)
  • expire:可选,16 进制 Unix 时间戳;不传默认当前时间 + 30 分钟
  • plugin:可选,插件名(默认 global,即使用全局继承 key)

返回示例:

{
"code": 0,
"message": "success",
"data": {
"type": "publish",
"plugin": "rtmp",
"streamPath": "live/test",
"expire": "6610f4a0",
"secret": "0123456789abcdef0123456789abcdef"
}
}

联系我们

微信公众号:不卡科技 微信公众号二维码
腾讯频道:流媒体技术 腾讯频道二维码
QQ 频道:p0qq0crz08 QQ 频道二维码
QQ 群:751639168 QQ 群二维码