spring
springboot - CORS
skyepodium
2022. 5. 29. 14:08
할때 마다 잊어버려서
Vue(SPA 클라이언트)에서 springboot로 요청을 보내고 응답받는데에는 Same Origin Policy에 의해 막혀있습니다.
이를 위해서는 CORS(교차출처 허용)을 추가해야합니다.
저는 패턴 /** 을 통해 모든 IP, 라우터를 해제했지만, 실제 프로덕션 레벨에서는 특정 도메인만 해제합니다.
스프링이 실행될 때 @SpringBootApplication 이 붙은 메서드부터 컴포넌트 스캔이 시작되고 @Configuration 어노테이션을 통해 설정내용이 빈에 등록됩니다.
// WebConfig.java
package com.example.vuespringbootinicis.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("/**")
.allowedOriginPatterns("*");
}
}