[트러블슈팅] spring-cloud-gateway CORS ERROR (403 Forbidden)

JeongHun·2023년 11월 2일
post-thumbnail

MSA를 사용해서 배포하는 과정에서 해당 오류가 발생했었다.
gateway yml에서 제대로 설정을 해주었고 로컬에서 백엔드, 프론트엔드 모두 정상 작동했었다.

BUT


뭔데 이거...
nginx 에러도 잘 확인이 안되고...
그래서 github에서 MSA 설정 관련 파일을 뒤졌고 몇몇 코드에서

        add-to-simple-url-handler-mapping: true

해당 설정이 추가되어있는걸 확인했다.

spring:
  cloud:
    gateway:
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials 
      globalcors:
        add-to-simple-url-handler-mapping: true
        corsConfigurations:
          '[/**]':
            allowedOrigins:
              - 'https://xxxxxxxxxx'
            allow-credentials: true
            allowedHeaders: '*'
            exposedHeaders: '*'
            allowedMethods:
              - POST
              - GET
              - PUT
              - PATCH
              - DELETE
              - OPTIONS

그래서 우리도 다음과 같이 추가했고 apigateway의 filter와 해당 서버에서 log가 찍히는것을 확인하였다.

cors-configuration

해당 spring 공식 문서에서 확인하면

To provide the same CORS configuration to requests that are not handled by some gateway route predicate, set the spring.cloud.gateway.globalcors.add-to-simple-url-handler-mapping property to true. This is useful when you try to support CORS preflight requests and your route predicate does not evalute to true because the HTTP method is options.

라는 것을 확인할 수 있다.

즉, route predicate 처리되지 않은 요청에 동일한 CORS 구성을 제공하는 것이다.

profile
coding study

1개의 댓글

comment-user-thumbnail
2023년 11월 2일

정리가 잘 되어있네요~ 감사합니다! ㅠㅠ

답글 달기