啓動應用項目:春雲 - 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處理這個問題,但我不知道爲什麼它不適合我。
感謝試圖回答,但我加了標籤的原因是因爲來自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
是的,但正如下面的@zeroflagL所說,這意味着您必須通過代理髮送所有請求。 – 2015-02-23 15:21:24