Thursday, 13 February 2020

Spring Cross Origin Issue

Spring Cross Origin Issue
  Web application running on 8080.
  Restful Service running on 9090 consuming the Webapplication service through the call ( http://localhost:9090/ramesh/index.html)
 
Issue : Cross Origin Issue will be happen. Spring Security is using the Same-Origin Policy by default

Solution: To fix this problem, we need to enable CORS (Cross-Origin Resource Sharing) for our backend services to allow requests coming from a
different origin


WebApplication running on 8080 modified like below
@SpringBootApplication
public class Application {
   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }
   @Bean
   public WebMvcConfigurer corsConfigurer() {
      return new WebMvcConfigurerAdapter() {
         @Override
         public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/products").allowedOrigins("http://localhost:8080");
         }
      };
   }
}

No comments:

Post a Comment