0

I know lots of posts in SO are talking about this. But I have tried half a day but still failed.

I have tried:

  1. add below method in my Application class:
@Bean 
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*");
        }
    };
}
  1. add a stand alone .java file in my project:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

3 add controller level annotation @CrossOrigin for both my controller class and the post mapping method:

@CrossOrigin
@PostMapping(value="/api/photoUpload/tester")
String tester(HttpServletRequest request) {

But my fiddler never show the wanted http resonse header. Any ideas.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39

1 Answers1

1

Add this Additional Configuration in your project.

@Configuration
public class AdditionalConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}


if it will not work. then you can try using the fundamental Servlet filter other than facilities provided by the Spring framework.

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class CORSFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers",
                "X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers");
        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}



ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
Pawan Maurya
  • 387
  • 2
  • 11
  • `import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter;`---is the importing right? – ZhaoGang Oct 14 '19 at 05:02
  • No error. I use fiddler to watch the http resonse. but I cannot see the `access-control-allow-origin`header. – ZhaoGang Oct 14 '19 at 05:26
  • 1
    please refer this may be it willl help https://stackoverflow.com/questions/32319396/cors-with-spring-boot-and-angularjs-not-working – Pawan Maurya Oct 14 '19 at 05:27
  • the link works. Update your answer and I wll accept it. @Pawan Maurya – ZhaoGang Oct 14 '19 at 06:04
  • can you please check with the update code @ZhaoGang? – Pawan Maurya Oct 14 '19 at 06:30
  • Similar post : https://stackoverflow.com/questions/56759527/cors-policy-conflict-in-spring-boot/56765503#56765503 – Ananthapadmanabhan Oct 14 '19 at 06:39