Stpring Boot

[ Spring Boot ] CORS 세팅 코드 를 알아보자

유바바아들 2023. 7. 12. 12:14

웹 개발을 하다 보면 한 번씩 꼭 만나는 수문장 같은 CORS를 해결할 수 있는 코드에 대해서 알아보자. 

1. CorsConfig 클래스 하나를 만들어준다. 아래 코드를 복붙한다.

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 CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

}

그럼 끝이다! 더추가할 옵션 및 설정해야 할 옵션이 있다면 수정해 주자.

728x90
LIST