通过Referer进行请求防盗链。
示例代码
function verifyReferer(referer) {
let regex = /ctyun/;
return regex.test(referer);
}
async function verifyHeaderParameters(request) {
//401表示鉴权不通过
let statuscode = 401
let result = 'Referer verification failed!'
try {
if(verifyReferer(request.headers.get("referer")) == true){
statuscode = 200
result = "Referer verification succeeded!"
}
} catch (error) {
result = error
}
return new Response(result, { "status": statuscode });
}
addEventListener('fetch', event => {
event.respondWith(verifyHeaderParameters(event.request))
})
示例预览
访问请求传入鉴权referer,鉴权通过。
相关参考
- 运行时API:addEventListener
- 运行时API:FetchEvent
- 运行时API:Web Standards
- 运行时API:Response
- 运行时API:Fetch