【发布时间】:2016-06-05 18:15:55
【问题描述】:
我已经实现了 zuul 服务器,它可以路由到我的所有微服务。我有一个单独的 UI 项目,它是有角度的。我正在尝试从 UI 应用程序向通过 Zuul 路由的特定微服务进行 AJAX 调用,但出现此错误。
XMLHttpRequest cannot load http://localhost:8006/user/v1/userassociations. 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:63342' is therefore not allowed access. The response had HTTP status code 403.
但是当我直接尝试访问托管在 http://localhost:4444/user/v1/userassociations 的本地主机中的 api 时,它工作得非常好。我将以下 CORS 配置添加到实际 api 中。
@Override
public void addCorsMappings(final CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("PUT", "GET", "DELETE", "OPTIONS", "PATCH", "POST");
}
只有当我尝试通过 zuul 访问 api 时才会出现问题,我尝试将相同的配置添加到 Zuul 但它不起作用。
我知道这是同源问题,但必须有一个解决方案,我们的微服务应该公开,以便任何人都可以从任何域发出请求。
下面是正在工作的 AJAX 调用:- 如果我通过 zuul 将 url 更改为 http://localhost:8006/user/v1/userassociations,我会得到交叉源。
var service = {};
service.userContextCall = function()
{
return $http({
url:'http://localhost:4444/user/v1/userassociations',
method: 'GET',
headers: {'Content-Type':'application/json',
'userContextId':'1234567890123456'}
}).success(function(response){
return response;
}).error(function(error){
return error;
});
};
return service;
我通过 Zuul 访问 api 时收到的标头。
Request URL:http://localhost:8006/user/v1/userassociations
Request Method:OPTIONS
Status Code:403 Forbidden
Remote Address:[::1]:8006
Response Headers
view source
Allow:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Content-Length:20
Date:Tue, 23 Feb 2016 18:51:15 GMT
Server:Apache-Coyote/1.1
X-Application-Context:apigateway:8006
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, usercontextid
Access-Control-Request-Method:GET
Connection:keep-alive
Host:localhost:8006
Origin:http://localhost:63342
Referer:http://localhost:63342/icpAccountHolder/app/index.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
当我直接点击 api 时的标题。
Request URL:http://localhost:4444/user/v1/userassociations
Request Method:GET
Status Code:200 OK
Remote Address:[::1]:4444
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:63342
Content-Type:application/json;charset=utf-8
Date:Tue, 23 Feb 2016 18:54:19 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Origin
X-Application-Context:userassociations-v1
Request Headers
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:4444
Origin:http://localhost:63342
Referer:http://localhost:63342/icpAccountHolder/app/index.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36
userContextId:1234567890123456
谁能帮我解决这个问题。
【问题讨论】:
-
CORS 不仅仅是
allowed methods。复制返回的标头样本。可能缺少一些 -
在我直接点击 api 和通过 zuul 时添加了两种场景的标题。
-
肯定缺少
Access-Control-Allow-Origin,可能还需要Access-Control-Allow-Credentials -
是的,我也看到了 Spring 应用程序在我的情况下 Zuul 也应该注意这一点,我什至为它添加了配置,但它仍然失败。我认为这更像是一个 java 方面的问题,而不是实际的 ajax 调用。
标签: angularjs ajax spring-cloud microservices netflix-zuul