2015-02-23 70 views
7

啓動應用項目:春雲 - Zuul代理是生產無「訪問控制允許來源」 Ajax響應

@SpringBootApplication 
@EnableZuulProxy 
public class ZuulServer { 

    public static void main(String[] args) { 
     new SpringApplicationBuilder(ZuulServer.class).web(true).run(args); 
    } 
} 

我YAML文件是這樣的:

server: 
    port:8080 

spring: 
    application: 
     name: zuul 

eureka: 
client: 
    enabled: true 
    serviceUrl: 
     defaultZone: http://localhost:8761/eureka/ 



zuul: 
    proxy: 
     route: 
     springapp: /springapp 

我有一個微服務應用程序(在端口8081上)稱爲springapp並有一些休息服務。下面是我的客戶端UI應用程序:

<html> 
    <head> 
     <title>TODO supply a title</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <script type="text/javascript" src="js/libs/jquery/jquery.min.js" ></script> 
    </head> 
    <body> 
     <script type="text/javascript"> 
      $.ajax({ 
       url: 'http://localhost:8080/zuul/springapp/departments', 
       type: 'GET' 
      }).done(function (data) { 
       consoe.log(data); 
       document.write(data); 
      }); 
     </script>   

    </body> 
</html> 

但我得到一個

XMLHttpRequest cannot load http://localhost:8080/zuul/springapp/departments. No 
    'Access-Control-Allow-Origin' header is present on the requested 
    resource. Origin 'http://localhost:8383' is therefore not allowed access. 

這個UI HTML5應用是http://localhost:8383/SimpleAPp/index.html。 CORS,CORS,CORS ...請幫助。順便說一句,http://localhost:8080/zuul/springapp/departments返回一個json列表,假設在瀏覽器地址欄上。 spring.io博客here表示不需要過濾器,因爲zuulproxy處理這個問題,但我不知道爲什麼它不適合我。

回答

20

將這段代碼添加到使用@EnableZuulProxy註釋的類中應該可以做到。

@Bean 
public CorsFilter corsFilter() { 
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    final 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); 
} 
1

這只是瀏覽器告訴你,你違反了它的共同來源政策(請參閱Wikipedia entry和互聯網上的大量資料,其中沒有一個與您添加的標籤真正相關)。您可以教瀏覽器,通過服務CORS飛行前檢查(例如,在Filter中)或通過代理加載HTML來從不同地址加載資源是正常的(提示:後者更容易,更少出錯) )。

+0

感謝試圖回答,但我加了標籤的原因是因爲來自Spring雲的@EnableZuulProxy。在spring.io的這篇文章[博客](http:// spring。io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv)他​​們說沒有必要使用你提到的Filter,因爲ZuulProxy做了一個消費客戶端的反向代理。希望這是有道理的。 – 2015-02-23 14:00:03

+1

是的,但正如下面的@zeroflagL所說,這意味着您必須通過代理髮送所有請求。 – 2015-02-23 15:21:24

5

當您的應用程序在http://localhost:8383上運行時,您只能進行AJAX調用http://localhost:8383。祖魯沒有也不能改變這種狀況。

Zuul能做的是映射例如http://localhost:8383/zuul/http://localhost:8080/zuul/。但是您的瀏覽器必須調用http://localhost:8383/zuul/springapp/departments,您必須配置該映射。

+0

非常感謝。這對我有用:)現在我的另一個問題是:如果你有一個角度js應用程序包裝在Cordova/Phonegap中,並在應用程序中使用靜態文件而不是從代理服務器提供文件,你會怎麼做?我問,因爲我的解決方案截至目前僅適用,因爲帶有ajax請求的靜態文件駐留在ZuulProxy服務器上。 – 2015-02-23 15:53:24

+0

我有一個針對上面的angular.js場景的解決方案。我添加了一個CORS過濾器,如[這裏](https://spring.io/guides/gs/rest-service-cors/)給ZuulProxy所示。因此,任何從與代理相同的域或從其他源進入ZuulProxy的請求都將被允許:) – 2015-02-23 16:40:31

+0

如果我假設正確,[CORS Filter](https://spring.io/guides/gs/rest- service-cors /)只適用於應用程序託管在根(/)上下文中。如果應用程序託管在任何其他上下文中,則需要回退以啓用CORS特定於該servlet容器。 – Stackee007 2015-03-06 15:18:35

0

我有一個類似的問題,Angular Web應用程序使用Spring Boot與Zuul和Spring Security實現的RESTful服務。

以上都不是解決方案。我意識到問題不在Zuul,而是在Spring Security。

由於官方文檔(CORS with Spring Security)指出,在使用Spring Security時,必須在Spring Security之前配置CORS。

最後,我能夠將Grinish尼泊爾的(見前答案)解決方案整合到一個可行的解決方案中。

事不宜遲,這裏是使CORS使用Spring Security和Zuul代碼: ```

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    //irrelevant for this problem 
    @Autowired 
    private MyBasicAuthenticationEntryPoint authenticationEntryPoint; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       //configure CORS -- uses a Bean by the name of  corsConfigurationSource (see method below) 
       //CORS must be configured prior to Spring Security 
       .cors().and() 
       //configuring security - irrelevant for this problem 
       .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
       .httpBasic() 
       .authenticationEntryPoint(authenticationEntryPoint); 

     //irrelevant for this problem 
     http.addFilterAfter(new CustomFilter(), 
       BasicAuthenticationFilter.class); 
    } 

    //The CORS filter bean - Configures allowed CORS any (source) to any 
    //(api route and method) endpoint 
    @Bean 
    CorsConfigurationSource corsConfigurationSource() { 
     final UrlBasedCorsConfigurationSource source = new  UrlBasedCorsConfigurationSource(); 
     final 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 source; 
    } 

    //configuring BA usernames and passwords - irrelevant for this problem 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws  Exception { 
     ... 
    } 
} 

```

+0

您是否在具有zuul的應用程序或具有微服務的應用程序中添加此類? – CuriousCoder 2017-12-01 14:25:00

相關問題