2016-06-20 86 views
0

我已经加入@ConfigurationCORS筛选春天启动不工作

在以下
@Bean 
public FilterRegistrationBean corsFilter() { 
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    CorsConfiguration config = new CorsConfiguration(); 
    config.setAllowCredentials(true); 
    config.addAllowedOrigin("*"); 
    config.addAllowedHeader("*"); 
    config.addAllowedMethod("OPTIONS"); 
    config.addAllowedMethod("HEAD"); 
    config.addAllowedMethod("GET"); 
    config.addAllowedMethod("PUT"); 
    config.addAllowedMethod("POST"); 
    config.addAllowedMethod("DELETE"); 
    config.addAllowedMethod("PATCH"); 
    source.registerCorsConfiguration("/**", config); 
    // return new CorsFilter(source); 
    final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); 
    bean.setOrder(0); 
    return bean; 
} 

除了:

@Bean 
    public WebMvcConfigurer mvcConfigurer() { 
     return new WebMvcConfigurerAdapter() { 
      @Override 
      public void addCorsMappings(CorsRegistry registry) { 
       registry.addMapping("/**").allowedMethods("GET", "PUT", "POST", "GET", "OPTIONS"); 
      } 
     }; 
    } 

我不要同时启用它们,

但是,当我使用CURL启动CORS预检请求我得到以下内容:

HTTP/1.1 200 OK 
< Server: Apache-Coyote/1.1 
< X-Content-Type-Options: nosniff 
< X-XSS-Protection: 1; mode=block 
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
< Pragma: no-cache 
< Expires: 0 
< X-Frame-Options: DENY 
< Access-Control-Allow-Origin: www.example.com 
< Vary: Origin 
< Access-Control-Allow-Credentials: true 
< Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH 
< Content-Length: 0 
< Date: Mon, 20 Jun 2016 08:26:06 GMT 
< 
* Connection #6 to host localhost left intact 

使用:

卷曲-H “来源:www.example.com” \ -H “X-auth的令牌:93f8ddb4-d2db-4c8f-b30e-a9fb8468437b” -H“访问控制,请求 - 方法:POST” \ -H “访问控制请求报头:X-要求,以” \ -X选项--verbose \ http://localhost:8181

我觉得理想的输出必须是这样的:

http://learntogoogleit.com/post/61327173902/cors-preflight-request-testing-in-curl

我得到 CORS预检模型不支持使用角cli ent

回答

0

你可以试试@CrossOrigin注解控制器类的顶部?

+0

这已经尝试过了 – Chetan