개발/기타

[SpringSecurity] Https same origin 에 대한 CORS 설정

아바바 2020. 6. 9. 23:39

기존 스프링 Security에서 same-origin의 경우 별도 CORS설정을 안해줘도 문제가 없었지만

origin이 Https가 되는 경우엔 cors 설정을 해줘야 한다. 

 

이렇게 Origin 헤더가 https인 경우 CORS 설정이 필요하다. 

Origin: https://test.io

 

또한 allow method 디폴트가 GET,HEAD, POST 이므로

PUT과 같은 method를 사용하는 경우

  public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
      @Override
      public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedMethods("*")
            .allowedOrigins("https://test.io")
            .allowedHeaders("*")
            .allowCredentials(true);
      }
    };
  }

registry.allowedMethods("*") 설정이 필요하다.