Skip to content

[FIX] lambda 환경 CORS 오류로 프론트엔드 API 호출 실패#411

Merged
softmoca merged 1 commit intodevelopfrom
410-fix-lambda-cors
Jan 18, 2026
Merged

[FIX] lambda 환경 CORS 오류로 프론트엔드 API 호출 실패#411
softmoca merged 1 commit intodevelopfrom
410-fix-lambda-cors

Conversation

@softmoca
Copy link
Member

🔗 관련 이슈

Related to #393

📋 작업 내용 요약

Lambda 환경에서 CORS 오류로 프론트엔드 API 호출이 실패하는 문제 수정.
기존 EC2의 Nginx CORS 설정을 Spring Security에서 처리하도록 변경.

주요 변경사항

  • SecurityConfigCorsConfigurationSource 빈 추가
  • Lambda 프로파일 전용 setHttpLambda() 메서드 분리 (.cors() 활성화)
  • template-dev.yaml에서 API Gateway CORS 설정 제거

💡 구현 방법

1. CORS 설정 빈 추가 (SecurityConfig.java)

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowedOriginPatterns(List.of("https://*.sopt.org", "http://localhost:*"));
    config.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
    config.setAllowedHeaders(List.of("Authorization", "Content-Type", "api-key"));
    config.setAllowCredentials(true);
    config.setMaxAge(3600L);
    
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return source;
}

2. Lambda용 Security 설정 분리

환경 메서드 CORS 처리
EC2 (dev, prod) setHttp() Nginx
Lambda (lambda-dev, lambda-prod) setHttpLambda() Spring

3. API Gateway CORS 비활성화

API Gateway의 Cors 섹션을 제거하여 OPTIONS 요청도 Lambda로 전달되도록 변경.
이를 통해 Spring의 CORS 설정이 모든 요청에 적용됨.

📸 스크린샷 / 실행 결과

수정 전

access-control-allow-origin: *
access-control-allow-credentials: (없음)
→ CORS 오류 발생

수정 후

access-control-allow-origin: https://dev.admins.sopt.org
access-control-allow-credentials: true
access-control-allow-methods: GET,POST,PUT,PATCH,DELETE,OPTIONS
→ 정상 동작

🧪 테스트

테스트 케이스

  • 단위 테스트 추가/수정
  • 통합 테스트 추가/수정
  • 수동 테스트 완료

테스트 시나리오

  1. curl로 OPTIONS preflight 요청 테스트
   curl -X OPTIONS "https://operation-api-dev.sopt.org/api/v1/auth/login" \
     -H "Origin: https://dev.admins.sopt.org" \
     -H "Access-Control-Request-Method: POST" \
     -v
  1. 프론트엔드(dev.admins.sopt.org)에서 로그인 테스트
  2. 브라우저 네트워크 탭에서 CORS 헤더 확인

🔍 리뷰 포인트

  • setHttp()setHttpLambda() 분리가 적절한지
  • allowedOriginPatterns 패턴이 보안상 적절한지 (*.sopt.org)

⚠️ 주의사항

  • Breaking Change 없음
  • DB 마이그레이션 필요
  • 환경 변수 추가/변경
  • 의존성 추가/변경

EC2 환경은 기존 Nginx CORS 설정 그대로 유지되어 영향 없음.
Lambda 환경만 Spring CORS 적용.

📝 추가 메모

EC2 vs Lambda CORS 처리 비교

항목 EC2 Lambda
CORS 처리 Nginx Spring Security
Origin 허용 $http_origin (동적) allowedOriginPatterns
Credentials true true
OPTIONS 처리 Nginx 204 반환 Spring CorsFilter

참고: 기존 Nginx 설정 (EC2)

add_header Access-Control-Allow-Origin $http_origin always;
add_header Access-Control-Allow-Credentials "true" always;

✅ PR 체크리스트

  • 코드 스타일 가이드 준수
  • Self Review 완료
  • 테스트 코드 작성 및 통과
  • 문서 업데이트 (필요 시)
  • 커밋 메시지 컨벤션 준수
  • 충돌(Conflict) 해결 완료

- SecurityConfig에 CorsConfigurationSource 빈 추가
- Lambda 프로파일용 setHttpLambda() 메서드 분리
- API Gateway CORS 설정 제거 (Spring에서 처리)

EC2 환경에서는 Nginx가 CORS를 처리했으나,
Lambda 환경에서는 Spring Security에서 직접 처리하도록 변경.

허용 Origin: *.sopt.org, localhost:*
허용 Credentials: true
@softmoca softmoca self-assigned this Jan 18, 2026
@softmoca softmoca linked an issue Jan 18, 2026 that may be closed by this pull request
4 tasks
@pull-request-size pull-request-size bot added the size/L 모듈 내부 리팩토링 label Jan 18, 2026
@softmoca softmoca merged commit de72f0d into develop Jan 18, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L 모듈 내부 리팩토링

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FIX] lambda 환경 CORS 오류로 프론트엔드 API 호출 실패

1 participant