서버에서 CORS 설정하기

사내 사이트 유지보수 하면서 그동안 cors 관련해서 할일도 없었는데
개발 관련 일을 계속하다보니 cors 관련 에러를 접함....
그리하여 조금 고생을 함.

1.php 설정
어느분 블로그를 보고 한건데 페이지가 기억이 안남..... 암튼 allow 된 사이트만 허용하게
하는것.


$http_origin = $_SERVER['HTTP_ORIGIN'];

$allowed_origin = array('http://사이트', 'https://사이트');

if (in_array($http_origin, $allowed_origin))
{
    header("Access-Control-Allow-Origin: {$http_origin}");
}



2. aws apigateway 설정
angular + s3 + lambda + apigateway 조합으로 사이트 만들었는데
여기에서 cors 삽질을 함.... (기본 지식이 부족해서)

암튼  요 링크에 cors 설정들이 대부분 있는거 같음.
https://enable-cors.org/server.html



요거보고 해결.
https://enable-cors.org/server_awsapigateway.html
내용들 :

  1. Log into API Gateway console
  2. Create all the REST resources that needs to be exposed with their methods before setting up CORS (if new resources/methods are created after enabling CORS, these steps must be repeated)
  3. Select a resource
  4. Add OPTIONS method, choose as integration type "mock"
  5. For each Method of a resource
  6. Go to Response Method
  7. Add all the response method that should be supported (i.e. 200, 500, etc.)
  8. For each response code set Response Headers to
    • X-Requested-With
    • Access-Control-Allow-Headers
    • Access-Control-Allow-Origin
    • Access-Control-Allow-Methods
  9. Go to Integration Response, select one of the created response codes, then Header Mappings
  10. Insert default values for headers
    example:
    • X-Requested-With: '*'
    • Access-Control-Allow-Headers: 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with'
    • Access-Control-Allow-Origin: '*'
    • Access-Control-Allow-Methods: 'POST,GET,OPTIONS'
    This operation has to be repeated for each method, including the newly created OPTIONS
  11. Deploy the API to a stage
  12. Check using http://client.cors-api.appspot.com/client that CORS requests have been successfully enabled





댓글

이 블로그의 인기 게시물

[DB] Mysql 숫자 기준으로 정렬하기.

[WEB] ASP.NET System.NullReferenceException: 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

[문서] excel 체크박스 삭제