I think most of the developers might have seen this issue in their development phase in local environment . we will discuss the solution in this post .
Tech Stack : ReactJS , SpringBoot with Rest
One of the solution worked for me is to add an extend class to WebMvcConfigurerAdapter.
The below solution will send the allow-access-control-origin as * which will give browser a signal to proceed further . for example if we are accessing the URI from http://localshot:1234 and if we don't implement below class our request won't proceed further because we are not getting Access-Control-Allow-Origin in Response header .
Tech Stack : ReactJS , SpringBoot with Rest
One of the solution worked for me is to add an extend class to WebMvcConfigurerAdapter.
The below solution will send the allow-access-control-origin as * which will give browser a signal to proceed further . for example if we are accessing the URI from http://localshot:1234 and if we don't implement below class our request won't proceed further because we are not getting Access-Control-Allow-Origin in Response header .
@Configurationpublic class WebConfigurerAdapter extends WebMvcConfigurerAdapter { @Override public void configurePathMatch(PathMatchConfigurer matcher) { matcher.setUseRegisteredSuffixPatternMatch(true); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/demo/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH") .allowCredentials(false).maxAge(3600); } @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public DispatcherServlet dispatcherServlet() { DispatcherServlet dispatcherServlet = new DispatcherServlet(); dispatcherServlet.setDispatchOptionsRequest(true); return dispatcherServlet; } }
No comments:
Post a Comment