流鉴权
Monibuca v6 支持两种流鉴权模式:
- 内置签名鉴权(v5 兼容)
- 自定义鉴权处理器(优先级更高)
鉴权按协议入口执行,当前已覆盖:
- 推流:
rtmp、webrtc(whip)、webtransport、srt、rtsp(record/announce) - 拉流:
rtmp、webrtc(whep)、flv/http-flv/ws-flv、hls、webtransport、srt、rtsp(describe/play)
启用内置鉴权
Section titled “启用内置鉴权”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
签名算法(v5 兼容)
Section titled “签名算法(v5 兼容)”签名计算:
secret = md5(key + streamPath + expireHex)其中:
key:publish.key或subscribe.keystreamPath:不含 query 的流路径(如live/test)expireHex:16 进制 Unix 时间戳(秒)
校验规则:
expire必须可解析为 16 进制时间戳,且未过期secret长度必须为 32secret与服务端计算值(忽略大小写)一致
URL 示例
Section titled “URL 示例”以 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=...自定义鉴权处理器
Section titled “自定义鉴权处理器”当你希望接入外部用户系统、权限中心或一次性票据时,可注册自定义鉴权处理器。
行为优先级:
- 自定义处理器(如果已注册)
- 内置
secret+expire鉴权
也就是说,自定义处理器一旦返回结果,将作为最终鉴权结果。
StreamManagerApi 提供:
set_stream_auth_handler(handler)
处理器签名:
- 输入:
StreamAuthRequestplugin_namestream_pathquery_stringparams(已解析 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 推流地址生成配合
Section titled “与 Admin 推流地址生成配合”Admin 的“推流地址”弹窗支持按 v5 规则自动生成:
expire(16 进制时间戳)secret(md5(key + streamPath + expire))
生成后会自动拼接到地址 query 中。
签名生成 API
Section titled “签名生成 API”服务端提供签名生成接口:
GET /api/secret/{publish|subscribe}/{streamPath...}?expire=<hex>&plugin=<pluginName>参数说明:
type:publish或subscribestreamPath:流路径(支持 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 群:751639168