边缘安全加速平台 EO 请求头鉴权

该示例通过校验请求头 x-custom-token 的值,若其值等于 token-123456 则允许访问,否则拒绝访问。使用边缘函数实现了简单的权限控制。

相关产品
边缘安全加速平台 EO 产品页,含版本规格、价格与购买入口。

一、示例代码

async function handleRequest(request) { const token = request.headers.get('x-custom-token'); if (token === 'token-123456') { return new Response('hello world'); } // Incorrect key supplied. Reject the request. return new Response('Sorry, you have supplied an invalid token.', { status: 403, });}addEventListener('fetch', event => { event.respondWith(handleRequest(event.request));});

二、示例预览

在浏览器地址栏中输入匹配到边缘函数触发规则的 URL,即可预览到示例效果。

·鉴权不通过,拒绝访问。

·鉴权通过,允许访问。

三、相关参考

·Runtime APIs: Headers

·Runtime APIs: Response

赞 (0)
上一篇 4天前
下一篇 4天前