Angular 6 + Spring Boot: Error: “from origin 'http://localhost:4200' has been blocked by CORS policy”

前端 未结 4 1915
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-19 16:59

I am trying to connect angular 6 project with spring boot application. When I run angular project, it constantly gives this error, although I have installed all the dependen

相关标签:
4条回答
  • 2021-01-19 17:24

    Issue is with the ordering of filters with spring boot. Add this annotation on your CORSFilter: @Order(Ordered.HIGHEST_PRECEDENCE).

    This will make sure that your CORSFilter has highest precendence of execution (most prior).

    0 讨论(0)
  • 2021-01-19 17:27

    When you are using Spring Boot, I would suggest you to add a CorsFilter bean in your configurations instead of introducing a CORS filter. Let me know if this helps.

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

    Happy Coding :)

    0 讨论(0)
  • 2021-01-19 17:33

    Please add a servlet filter and add the following code. It should work. Adding "Access-Control-Allow-Headers", "*" is mandatory. Creation of proxy.conf.json is not needed.

    @Component
    

    @Order(1) public class MyProjectFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,PUT,OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Max-Age", "86400");
        chain.doFilter(req, res);
    }
    

    }

    0 讨论(0)
  • 2021-01-19 17:35

    This error is coming because request contains some additional headers which are not mentioned in your CORS filter configuration.

    For adding CORS support during development, I generally prefer adding below spring configuration file. You need to have app.cors.enabled key with value true in your application configuration file (application.properties) also to make it work.

    import org.springframework.context.annotation.Bean;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    @ConditionalOnProperty(name = "app.cors.enabled")
    /**
    * If the value of the key "app.cors.enabled" is true in application.properties file,
    * then only this configuration will be enabled.
    * 
    */
    public class SpringConfig {
    
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/*").allowedHeaders("*").allowedOrigins("*").allowedMethods("*")
                            .allowCredentials(true);
                }
            };
        }
    }
    

    Make sure for production environment, you remove app.cors.enabled key from your configuration file or set its value to false. If you need CORS support in production environment also, make sure you use fixed values instead of allowing all values using *

    Once you do that, there is no need of @CrossOrigin annotation on your controller.

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