async function handleRequest(request) { // 远程鉴权 API 地址 const checkAuthUrl = 'https://www.example.com/'; // 发起远程鉴权 const checkAuthRes = await fetch(checkAuthUrl); // 鉴权通过,正常访问资源 if (checkAuthRes.status === 200) { return fetch(request, { headers: request.headers, }); } // 鉴权不通过,禁止访问资源 return new Response(null, { status: 403 });}addEventListener('fetch', e => { e.respondWith(handleRequest(e.request));});
一、示例预览
在 PC 端与移动端的浏览器地址栏中输入匹配到边缘函数触发规则的 URL(如:https://example.com/app/index.html),即可预览到示例效果。
·鉴权通过,正常访问资源。
·鉴权不通过,禁止访问资源。
二、相关参考
·Runtime APIs: Fetch
·Runtime APIs: Response