A CORS preflight block error is displayed in the browser console when a client-side application hosted on https://webapp.example.com sends a request to an Amazon API Gateway REST API. The API is configured with a custom Lambda Authorizer. Investigation reveals that the error occurs only when the authorizer rejects requests containing expired JSON Web Tokens (JWTs), which prevents the browser from reading the actual 401 Unauthorized status code. How should the developer resolve this issue?
- AModify the custom Lambda Authorizer function to return the Access-Control-Allow-Origin header within the validation context output.
- Configure Gateway Responses in API Gateway for the Unauthorized and Access Denied response types to return the required Access-Control-Allow-Origin header.Cevap
- CConfigure the S3 bucket policy hosting the frontend application to allow CORS headers from the API Gateway endpoint.
- DChange the integration type of the API Gateway method to Lambda Custom integration and handle the HTTP headers directly.
Cevap
Configure Gateway Responses in API Gateway for the Unauthorized and Access Denied response types to return the required Access-Control-Allow-Origin header.
Configuring Gateway Responses in API Gateway for the Unauthorized and Access Denied response types ensures that when a request fails authentication at the custom authorizer level, the response returned by API Gateway contains the necessary Access-Control-Allow-Origin headers. This allows the browser to process the 401 or 403 HTTP status code instead of blocking the response due to CORS policy violations.
Adım Adım Çözüm
Anahtar Kavram
API Gateway Gateway Responses are used to customize responses and inject CORS headers for requests that fail before reaching the backend integration.