1

我们正在尝试通过 OpenShift 设置 3scale 平台来管理 REST 服务和 JavaScript Web 应用程序之间的 API 访问。身份验证应使用放置在 HTTP 标头中的用户密钥进行管理。这两个应用程序可通过不同的 URL 访问:

JS web application:     http://siteA.example.com
REST API application:   http://siteB.example.com

所以我们使用 CORS 在 webapp 上实现跨域资源。这引入了浏览器发送的几个 OPTIONS 飞行前请求,没有用户密钥标头,因此从 3scale 收到 HTTP 403 错误。

有没有办法避免这种行为?

4

4 回答 4

1

在使用 AWS AMI 时,我在 3scale 中遇到了同样的问题,并且能够通过将 app_key 和 app_id 添加到选项请求的允许标头来解决它。

在测试请求在邮递员中工作但不能通过 Chrome 工作。在我的情况下,当浏览器发出预检选项检查时,它会导致拒绝,因为 CORS 默认值不允许 app_key 和 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 ####

然后将 cors.conf 包含在nginx.conf的任何“location /”块下:

...
location / {
include /opt/openresty/nginx/conf/cors.conf;
  set $provider_key null;
  set $cached_key null;
...

进行此更改后,我的浏览器就能够成功发出请求。

Nginx 文档上的CORS被用作基线,我注意到只有 OPTIONS 部分需要修改以获得所需的结果。

于 2018-01-05T17:24:48.940 回答
1

如果您无法在应用程序级别处理它,那么您可以执行 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;
    }
    ...
}

通过http://blog.rogeriopvl.com/archives/nginx-and-the-http-options-method/

于 2017-05-30T14:57:50.227 回答
0

当环境变量 APICAST_PATH_ROUTING 设置为 true,并且 APIcast 公开多个服务时,如果没有为所有服务启用 CORS 策略,则预检 OPTIONS 请求将收到 403(禁止)响应。

为 3scale 中的所有服务/api 设置 CORS 策略。

来自 RedHat 的更多信息: https ://issues.jboss.org/browse/THREESCALE-3063

于 2019-09-23T20:27:09.360 回答
0

3scale 网关现在支持开箱即用的 CORS 策略定义。更多信息在这里:https ://access.redhat.com/documentation/en-us/red_hat_3scale_api_management/2.8/html/administering_the_api_gateway/apicast_policies#cors

于 2020-06-29T06:29:20.773 回答