在404上没有添加Spring CORS头文件

时间:2017-08-04 20:20:21

标签: spring spring-mvc spring-boot cors spring-restcontroller

我在春天设置了CORS:

@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("http://localhost:3000").allowedMethods("GET","POST","PUT","DELETE", "OPTIONS");
        }
    };
}

这适用于所有端点和错误处理程序,但不适用于404处理程序。我设置了属性以确保spring在发现处理程序时抛出异常,所以我认为它将以与其他异常相同的方式处理

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false

我从浏览器中的fetch调用中获得此响应:

OPTIONS http://localhost:8080/badEndpoint 404 ()
localhost/:1 Fetch API cannot load http://localhost:8080/badEndpoint. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

为什么没有在404处理程序上应用CORS标头的任何想法?

0 个答案:

没有答案