2016-06-20 5 views
0

Ich habe folgende unter @ConfigurationCORS-Filter für das Frühjahr Boot nicht funktioniert

@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; 
} 

Neben hinzugefügt:

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

ich beide nicht aktivieren,

Aber wenn Ich feuere die CORS Preflight-Anfrage mit CURL Ich bekomme Folgendes:

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 

Verwendung:

curl -H "Herkunft: www.example.com" \ -H "x-Auth-Token: 93f8ddb4-d2db-4c8f-B30E-a9fb8468437b" -H „Access-Control-Request- Methode: POST“\ -H "Access-Control-Request-Header: X-Requested-With" \ -X OPTIONS --verbose \ http://localhost:8181

ich fühle mich der ideale Ausgang etwas sein, hat wie:

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

Ich bekomme die CORS Preflight-Modell nicht mit eckigen CLI unterstützt ent

Antwort

0

Können Sie @CrossOrigin Annotation oben in Ihrer Controller-Klasse versuchen?

+0

Dies wurde bereits versucht – Chetan