Spring Global CORS configuration not working but Controller level config does

后端 未结 10 1131
臣服心动
臣服心动 2020-12-14 06:25

I am trying to configure CORS globally via WebMvcConfigurerAdapter shown below. To test I am hitting my API endpoint via a small node app I created to emulate a

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 06:50

    I was facing the same issue and after setting the maxAge attribute everything started working ok!

    @Bean
    public WebMvcConfigurer CORSConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedHeaders("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD")
                        .maxAge(-1)   // add maxAge
                        .allowCredentials(false);
            }
        };
    }
    

    if you check the CrossOrigin annotation it has a default value assigned to that attribute

    /**
     * 

    By default this is set to {@code 1800} seconds (30 minutes). */ long maxAge() default -1;

提交回复
热议问题