すべてのプロダクト
Search
ドキュメントセンター

Edge Security Acceleration:URL 認証の設定

最終更新日:Apr 18, 2025

Edge Security Acceleration(ESA) Edge Routine テンプレートは、3 つの URL 認証方式を提供します。認証に失敗した URL はブロックされ、リソースが保護されます。このトピックでは、URL 認証の設定とデバッグに役立つコード例とパラメーターの説明を紹介します。

認証シナリオ

  • ファイルへのアクセス

  • API リクエストの実行

認証方式

認証方式 A、B、および C は、MD5 を使用してデータを暗号化します。クライアントの MD5 ハッシュは、配信拠点(POP)に保存されているハッシュと比較されます。一致する場合、リソースが返されます。一致しない場合は、アクセスが拒否されます。

方式 A

  • 認証 URL 形式: http://DomainName/Filename?auth_key={timestamp}-{rand}-{uid}-{md5hash}. URL フィールドの詳細については、「フィールドの説明」をご参照ください。

  • サンプルコード

    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. URL フィールドの詳細については、「フィールドの説明」をご参照ください。

  • サンプルコード

    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 認証の場合、コンテンツはパスにあり、再構築する必要があります
      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. URL フィールドの詳細については、「フィールドの説明」をご参照ください。

  • サンプルコード

    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 認証の場合、コンテンツはパスにあり、再構築する必要があります。
      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 Web サイトの DNS レコード。

  • timestamp: 認証サーバーが認証 URL を生成した時刻。認証 URL の有効期限は、認証 URL の Time To Live(TTL)とともに決定されます。時刻は UTC + 08:00 で、YYYYMMDDHHMM 形式に従います。

    説明

    ほとんどの場合、認証 URL の TTL は ESA で設定されます。場合によっては、計算プロセス中に増加します。その場合、timestamp は、UNIX タイムスタンプに増加した時間を加算して計算されます。認証 URL の実際の有効期限は、タイムスタンプに ESA で設定された TTL を加算したものです。

  • 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: このリクエストの認証情報。timestampranduid、および md5hash で構成されます。