全部產品
Search
文件中心

Edge Security Acceleration:配置URL鑒權

更新時間:Jan 13, 2026

本文為您介紹邊緣安全加速 ESA邊緣函數的函數模板中的三種鑒權方式的實現規範,涵蓋簽名演算法、URL格式、欄位定義及服務端校正邏輯,適用於開發人員在檔案訪問、API請求等情境中整合安全鑒權能力。文檔通過程式碼範例、參數說明,協助您配置與調試鑒權。

鑒權效果

阻止鑒權失敗的URL訪問您的資源,保護您的來源站點資源。

鑒權方式

A、B、C三種鑒權方式通過MD5對不同字串進行加密後,對用戶端和邊緣節點儲存計算的MD5值進行對比,結果一致則返回請求資源,結果不一致則拒絕訪問。

鑒權方式A

  • 鑒權URL格式http://DomainName/Filename?auth_key={timestamp}-{rand}-{uid}-{md5hash},組成說明參考欄位說明

  • 範例程式碼

    import { createHash } from "node:crypto";
    
    async function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const delta = 3600;
    
      const authKeyTypeA = url.searchParams.get('auth_key');
    
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
    
      if (!authKeyTypeA) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const [timestamp, rand, uid, signature] = authKeyTypeA.split('-');
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      const signString = [path, timestamp, rand, uid, privateKey].join('-');
    
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      // 如果資源在其他網域名稱,則請求後返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // 大多情況下回源即可
      return fetch(request.url)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 測試與驗證

    • 通過命令列工具,測試可鑒權通過的測試URL:curl -I http://esa.aliyun.top/video/test.mp4?auth_key=1743388566-61b20a42d14f403ba3790d1b82502027-1-ee1c2c463a41ef4d72fcff5987f1e08c 。返回結果HTTP響應碼為200,表示鑒權成功。

      image

    • 通過命令列工具,測試不符合可鑒權的測試URL:curl -I http://esa.aliyun.top/test.mp4。返回結果HTTP響應碼為401,表示鑒權失敗。

      image

鑒權方式B

  • 鑒權URL格式http://DomainName/{timestamp}/{md5hash}/FileName,組成說明參考欄位說明

  • 範例程式碼

    import { createHash } from "node:crypto";
    
    function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const parts = path.split('/');
      const delta = 3600;
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
      const timestamp = parts[1];
      const signature = parts[2];
    
      if (!timestamp || !signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const filePath = '/' + parts.slice(3).join('/');
      const signString = [privateKey, timestamp, filePath].join('');
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 403 });
      }
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      // 如果資源在其他網域名稱,則請求後返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // B鑒權的內容在path中,需要重新拼接
      return fetch(url.origin + filePath)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 測試與驗證

    • 通過命令列工具,測試符合可鑒權通過的測試URL:http://esa.aliyun.top/1743391454/f1415e282ae5ae650455e7b525231eff/test.mp4。返回結果HTTP響應碼為200,表示鑒權成功。

      image

    • 通過命令列工具,測試不符合可鑒權通過的測試URL:http://esa.aliyun.top/test.mp4。返回結果HTTP響應碼為401,表示鑒權失敗。

      image

鑒權方式C

  • 鑒權URL格式http://DomainName/{md5hash}/{timestamp}/FileName,組成說明參考欄位說明

  • 範例程式碼

    import { createHash } from "node:crypto";
    
    function handleRequest(request) {
      const url = new URL(request.url);
      const path = url.pathname;
      const parts = path.split('/');
      const delta = 3600;
      const privateKey = 'your_secret_key'
      const currentTimestamp = Math.floor(Date.now() / 1000);
      const signature = parts[1];
      const hexTime = parts[2];
      const timestamp = parseInt(hexTime, 16);
    
      if (!hexTime || !signature) {
        return new Response('Unauthorized', { status: 401 });
      }
    
      const filePath = '/' + parts.slice(3).join('/');
      const signString = [privateKey, filePath, hexTime].join('-');
      const md5 = createHash('md5').update(signString).digest('hex');
    
      if (md5 !== signature) {
        return new Response('Unauthorized', { status: 403 });
      }
    
      if (currentTimestamp > parseInt(timestamp)+ delta) {
        return new Response('Link expired', { status: 403 });
      }
    
      // 如果資源在其他網域名稱,則請求後返回
      // const yourUrl = `https://your-dcdn-domain.com${path}${url.search}`
      // const cdnResponse = await fetch(yourUrl, request)
      // return new Response(cdnResponse.body, cdnResponse)
    
      // C鑒權的內容在path中,需要重新拼接
      return fetch(url.origin + filePath)
    }
    
    export default {
      async fetch(request) {
        return handleRequest(request)
      }
    }
  • 測試與驗證

    • 通過命令列工具,測試符合可鑒權通過的測試URL:http://esa.aliyun.top/e59c904c85f19a48413b6283fc9d2f5a/1743400480/test.mp4。返回結果HTTP響應碼為200,表示鑒權成功。

      image

    • 通過命令列工具,測試不符合可鑒權通過的測試URL:http://esa.aliyun.top/test.mp4。返回結果HTTP響應碼為401,表示鑒權失敗。

      image

欄位說明

  • DomainName:ESA網站的DNS記錄。

  • timestamp:簽算伺服器產生鑒權URL的時間,與鑒權URL有效時間長度共同控制鑒權URL的失效時間。時間點取自簽算伺服器的“UTC+8”時間,格式為:YYYYMMDDHHMM。

    說明

    多數情況下,鑒權URL的有效時間長度為ESA配置有效時間長度。有時在簽算增加鑒權URL的有效時間長度的,此時,timestamp=Unix時間戳記+增加的時間長度;鑒權URL實際有效時間長度=timestamp+ESA配置的時間長度。

  • FileName:實際回源訪問的檔案路徑名,鑒權時FileName需以/開頭。

  • md5hash:通過MD5演算法計算出的驗證串,由數字0~9和小寫英文字母a~z混合組成,固定長度32。

    md5hash值的計算方式:

    • A鑒權方式:getMd5(FileName + "-" + timestamp + "-" + rand + "-" + uid + "-" + your_secret_key)

    • B鑒權方式:getMd5(your_secret_key + timestamp + FileName)

    • C鑒權方式:getMd5(your_secret_key + "-" + FileName + "-" + timestamp)

    說明

    上面的getMd5函數需要手動實現。

  • rand:隨機數。建議使用UUID,不能包含中劃線-,例如:477b3bbc253f467b8def6711128c7bec

  • uid:使用者ID,根據業務設定。

  • auth_key:本次請求的鑒權資訊,由timestamp、rand、uid和md5hash組成。