API Gateway+LambdaでヘッダにSet-Cookieを複数設定する方法

API Gateway+LambdaでヘッダにSet-Cookieを複数設定する方法

CloudFrontの署名付きCookieを使用しようとしたのですが、3つSet-Cookieを設定する必要があります。

ところが、API GatewayはSet-Cookieを複数設定することをサポートしていないようです。Discussion Forums

ここで抜け道を探しました。https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2

API Gatewayのヘッダーは大文字小文字が区別される

API Gatewayのヘッダは大文字小文字が区別されます。

なので、以下は全て別のヘッダとなります。

Set-Cookie
SET-CookiE
SeT-CookIe

これでLambdaで設定してみます。簡単にするためにLambdaプロキシの統合にチェックを入れています。

Lambdaのindex.jsです。(ハンドラ:index.handler)

exports.handler = async (event) => {
  const response = {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json; charset=utf-8',
      'Set-Cookie': 'CloudFront-Expires=xxxx', // 複数指定
      'Set-CookiE': 'CloudFront-Signature=yyyy', // 複数指定
      'Set-CookIe': 'CloudFront-Key-Pair-Id=zzzz' // 複数指定
    },
    body: JSON.stringify('test'),
  };
  return response;
};

API Gateway + Lambdaの構成でデプロイします。

ヘッダ情報の確認方法

ChromeのNetworkタブで確認できます。

API Gateway+LambdaでヘッダにSet-Cookieを複数設定する方法

これでAPI Gateway + Lambdaで署名してもらって、CloudFrontに署名付きCookieでアクセスすることが可能です。

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました