Spring CORS No 'Access-Control-Allow-Origin' header is present

后端 未结 11 1208
半阙折子戏
半阙折子戏 2020-11-30 18:21

I am getting the following problem after porting web.xml to java config

No \'Access-Control-Allow-Origin\' header is present on the requested resource. Origi         


        
11条回答
  •  臣服心动
    2020-11-30 18:48

    Following on Omar's answer, I created a new class file in my REST API project called WebConfig.java with this configuration:

    @Configuration
    @EnableWebMvc
    public class WebConfig extends WebMvcConfigurerAdapter {
    
      @Override
      public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*");
      }
    } 
    

    This allows any origin to access the API and applies it to all controllers in the Spring project.

提交回复
热议问题