所以我正在使用Express,目前我有一条路由将视频流传输到客户端,但是我想为此添加身份验证。当前,在客户端,我仅使用标准视频元素,因此无法发送授权标头。实际上,我在图片下载时遇到了类似的问题,这使我创建了一条路径,该路径创建了一个临时的一次性令牌,该令牌会在两秒钟后失效,并改用该方法。但是视频流的问题在于,为了能够快进/快退,必须将Accept-Ranges标头设置为字节。 (Streaming a video file to an html5 video player with Node.js so that the video controls continue to work?),因此将多次调用视频的get请求,有时仅调用一次,有时甚至是三个或更多次,这使得我用于图像的临时令牌无法正常工作在这种情况下。确保这条路线的最佳方法是什么?我想避免使用会在一天之内到期的临时令牌,或者尽可能避免使用cookie来发送授权标头。
console.log("stream request", req.params.id, req.headers)
const ID = req.params.id;
const bucket = new mongoose.mongo.GridFSBucket(conn.db, {
chunkSizeBytes: 1024 * 255,
})
let currentFile = await conn.db.collection("fs.files")
.findOne({"_id": ObjectID(ID)})
const fileSize = currentFile.length
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4',
'Accept-Ranges': 'bytes',
}
res.writeHead(200, head)
bucket.openDownloadStream(ObjectID(ID)).pipe(res)
这是我的快速路线。
const config = {
headers: {'Authorization': "Bearer " + window.localStorage.getItem("token")}
};
axios.get('http://' + currentURL +'/file-service/download/get-token-video',config)
.then((response) => {
const tempToken = response.data.tempToken;
const finalUrl = "http://" + currentURL + `/file-service/stream-video/${this.props.popupFile.id}/${tempToken}`
this.video.src = finalUrl;
console.log("got video")
}).catch((err) => {
console.log(err)
})
这是客户端代码。