Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[trobleshouting] EC2, CORS #46

Open
YangDaJin0104 opened this issue Jan 22, 2025 · 0 comments
Open

[trobleshouting] EC2, CORS #46

YangDaJin0104 opened this issue Jan 22, 2025 · 0 comments
Assignees

Comments

@YangDaJin0104
Copy link
Collaborator

❓문제 개요

  1. AWS EC2 접속 안되는 문제
  2. build가 되지 않는 문제

📌 오류 코드 및 오류 메시지

  1. timeout이 뜨거나 post = 22 가 열려있지않다는 오류
  2. build 무한 로드

🔎 원인 분석

  1. CPU 점유율이 상당히 높은 수치로 찍힘

📚 해결책

  1. swap
  2. 기다려보기...

👋 주의 사항

  1. 초반 빌드가 오래걸릴 수 있지만, 10분이 넘는다면 문제가 있는 상황이 있으니 문제를 빠르게 수정할 수 있도록 하자.

❓문제 개요

  1. CORS 문제 발생

📌 오류 코드 및 오류 메시지

java.lang.IllegalArgumentException: When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.

🔎 원인 분석

  1. 서버 배포시 CORS를 생각하지 않고 배포하였다.

📚 해결책

package sopt.jeolloga.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("") // 허용할 도메인 설정
                .allowedHeaders("Authorization", "Content-Type")
                .exposedHeaders("Custom-Header")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
  1. WebMvcConfigurer 인터페이스를 구현하여 Spring MVC 관련 설정을 커스터마이징 후 addCorsMappings 메서드로 특정 경로에 대한 CORS 정책을 설정
  2. allowedHeaders에 요청에서 전달 가능한 헤더를 설정하고, exposedHeaders에는 클라이언트가 접근 가능한 헤더를 설정

👋 주의 사항

  1. allowCredentials(true)와 allowedOrigins("*") 동시 사용 금지
  2. 필요한 OPTIONS 요청을 피하려면 브라우저가 캐시할 수 있는 충분한 maxAge를 설정할 것
  3. 웹 배포시에는 CORS 설정을 잊지않고 할수있도록 꼭 기억하기
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant