Rest 3标度的CORS
我们正试图在OpenShift上建立一个3scale平台来管理REST服务和JavaScript web应用程序之间的API访问。应使用HTTP头中的用户密钥管理身份验证。 这两个应用程序可在不同的URL上访问:Rest 3标度的CORS,rest,api,http,nginx,3scale,Rest,Api,Http,Nginx,3scale,我们正试图在OpenShift上建立一个3scale平台来管理REST服务和JavaScript web应用程序之间的API访问。应使用HTTP头中的用户密钥管理身份验证。 这两个应用程序可在不同的URL上访问: JS web application: http://siteA.example.com REST API application: http://siteB.example.com 因此,我们正在使用CORS在webapp上实现跨源资源。这引入了几个选项,即浏览器在不使
JS web application: http://siteA.example.com
REST API application: http://siteB.example.com
因此,我们正在使用CORS在webapp上实现跨源资源。这引入了几个选项,即浏览器在不使用用户密钥头的情况下发送飞行前请求,从而从3scale接收HTTP 403错误
有没有办法避免这种行为 如果不能在应用程序级别处理它,那么可以使用nginx If语句来处理它
location / {
if ($request_method = OPTIONS ) {
add_header Access-Control-Allow-Origin "http://example.com";
add_header Access-Control-Allow-Methods "GET, OPTIONS";
add_header Access-Control-Allow-Headers "Authorization";
add_header Access-Control-Allow-Credentials "true";
...
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
...
}
通过在使用AWS AMI时,我在3scale中遇到了同样的问题,并且能够通过将app_key和app_id添加到允许的选项请求头中来解决这个问题 在测试中,请求在postman中起作用,但在Chrome中不起作用。在我的例子中,当浏览器发出飞行前选项检查时,它导致拒绝,因为CORS默认不允许使用app_键和app_id头 可以通过在“Access Control Allow headers”(访问控制允许标头)标头的末尾添加这些标头的条目来添加对这些标头的支持。我将此配置作为一个单独的文件,名为cors.conf:
#### CORS ####
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,app_id,app_key';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
#### CORS END ####
...
location / {
include /opt/openresty/nginx/conf/cors.conf;
set $provider_key null;
set $cached_key null;
...
然后,cors.conf被包含在nginx.conf中的任何“location/”块下:
#### CORS ####
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,app_id,app_key';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
#### CORS END ####
...
location / {
include /opt/openresty/nginx/conf/cors.conf;
set $provider_key null;
set $cached_key null;
...
一旦进行了此更改,我的浏览器就能够成功地发出请求
该文档被用作基线,我注意到只有选项部分需要修改以获得所需的结果。当环境变量APICAST\u PATH\u ROUTING设置为true,并且APICAST公开多个服务时,如果没有为所有服务启用CORS策略,飞行前选项请求将收到403(禁止)响应 在3scale中的所有服务/API上设置CORS策略 来自RedHat的更多信息:
3缩放网关现在支持现成的CORS策略定义。更多信息请参见:这是一个界限。您应该扩展您的答案,在此处包含尽可能多的信息,并使用该链接仅供参考。