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

后端 未结 11 1121
半阙折子戏
半阙折子戏 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:44

    We had the same issue and we resolved it using Spring's XML configuration as below:

    Add this in your context xml file

    <mvc:cors>
        <mvc:mapping path="/**"
            allowed-origins="*"
            allowed-headers="Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Authorization, X-Requested-With, requestId, Correlation-Id"
            allowed-methods="GET, PUT, POST, DELETE"/>
    </mvc:cors>
    
    0 讨论(0)
  • 2020-11-30 18:47

    I have found the solution in spring boot by using @CrossOrigin annotation.

    @RestController
    @CrossOrigin
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 18:50

    Helpful tip - if you're using Spring data rest you need a different approach.

    @Component
    public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {
    
     @Override
     public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.getCorsRegistry().addMapping("/**")
                .allowedOrigins("http://localhost:9000");
      }
    }
    
    0 讨论(0)
  • 2020-11-30 18:50

    If you are using Spring Security ver >= 4.2 you can use Spring Security's native support instead of including Apache's:

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

    The example above was copied from a Spring blog post in which you also can find information about how to configure CORS on a controller, specific controller methods, etc. Moreover, there is also XML configuration examples as well as Spring Boot integration.

    0 讨论(0)
  • 2020-11-30 18:50

    Omkar's answer is quite comprehensive.

    But some part of the Global config part has changed.

    According to the spring boot 2.0.2.RELEASE reference

    As of version 4.2, Spring MVC supports CORS. Using controller method CORS configuration with @CrossOrigin annotations in your Spring Boot application does not require any specific configuration. Global CORS configuration can be defined by registering a WebMvcConfigurer bean with a customized addCorsMappings(CorsRegistry) method, as shown in the following example:

    @Configuration
    public class MyConfiguration {
    
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/api/**");
                }
            };
        }
    }
    

    Most answer in this post using WebMvcConfigurerAdapter, however

    The type WebMvcConfigurerAdapter is deprecated

    Since Spring 5 you just need to implement the interface WebMvcConfigurer:

    public class MvcConfig implements WebMvcConfigurer {
    

    This is because Java 8 introduced default methods on interfaces which cover the functionality of the WebMvcConfigurerAdapter class

    0 讨论(0)
提交回复
热议问题