프로젝트 중 cors 이슈가 있어서 원인 파악을 하며 알게 되었습니다. http://honeymon.io/tech/2018/03/13/spring-boot-mvc-controller.html 블로그를 참고하여 작성하였습니다.
기존에 cors 이슈를 해결하기 위해 WebMvcConfigurer 를 impliments 받은 클래스를 사용하고 있었다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private CustomResolver customResolver;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/")
.setCachePeriod(20);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns(
"<http://localhost>:[*]",
"<https://localhost>:[*]",
"........"
)
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE", "HEAD")
.allowedHeaders("*")
.allowCredentials(true);
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(0,customResolver);
}
}
cors와 custom annotation 처리를 위한 customResolver를 등록하여 쓰고 있었는데 어느 시점부터 cors문제가 재발하여 원인을 파악하며 알게 되었다.
WebMvcConfigurationSupport 를 extends 받은 클래스 생성이후 WebConfig.java가 제대로 작동하지 않게 되었다. 이를 바탕으로 알아본 사실을 정리해 본다.
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
}
- 스프링 MVC자동구성은 WebMvcAutoConfiguration 이 담당한다.
- @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) 이부분에서 WebMvcConfigurationSupport가 없을때 이 구성이 활성화 된다고 한다.
- 구성이 활성화 될때 WebMvcConfigurer.class 를 바탕으로 구성된다.
따라서 WebMvcConfigurationSupport 를 상속받은 클래스 생성이후로 이 문제가 발생하였고 이 클래스를 제거한 이후로 정상적으로 모두 돌아 오게되었다.