MFS
消息通知 鉴权验证流程 MFS消息通知鉴权验证流程,如下所示:
视频点播 服务会根据当前时间生成一个时间戳timestamp,使用2.b中配置的密钥,根据HmacSHA256(VOD_{timestamp}_{body}, key)计算出签名字符串。 签名字符串和时间戳会在HTTP的消息头中发送至端侧,分别为header[auth_sign]和header[auth_timestamp]。其中,body为消息体中的message属性。 客户收到返回消息后,按照VOD_{timestamp}_{body}格式拼接字符串,再使用消息头中的密钥计算HmacSHA256(VOD_{timestamp}_{body}, key)生成签名字符串和消息头中的签名字符串比较是否一致。如果一致,则鉴权成功。 对应的Java示例代码,如下所示: // 时间戳,消息头中参数header[auth_timestamp]
long reqTime = 1l;
// 消息体中的message属性
String message = "";
// 密钥
String key = "";
String algorithm = "HmacSHA256";
// 生成HmacSHA256密钥
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), algorithm);
// 创建Mac对象并初始化
Mac mac = Mac.getInstance(algorithm);
mac.init(signingKey);
String content = "VOD_"+reqTime+"_" + message;
// 执行HmacSHA256加密
byte[] rawHmac = mac.doFinal(content.getBytes(StandardCharsets.UTF_8));
// 将加密结果转换为十六进制字符串
StringBuilder sign = new StringBuilder();
for(byte b : rawHmac) {
sign.append(String.format("%02x", b));
}
System.out.println(sign);