无法通过 AngularJS 请求 WSO2 API(激活 CORS)
Not able to request WSO2 API (with CORS activated) via AngularJS
当我的客户端 AngularJS 应用程序试图访问通过 WSO2 API 管理器 (v2.0.0) 公开的一些 REST API 时,我得到了这个响应(IP,端口替换为 "am_host" 和 "am_port") :
Refused to set unsafe header "Origin"
XMLHttpRequest cannot load http://am_host:am_port/ReferentielInfoConso/offres-tarifaires/identifiants. 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:8000' is therefore not allowed access.
当我检查 Chrome 开发者工具中的 headers 时,我可以确认 "Access-Control-Allow-Origin" header 不存在于对 "OPTIONS" 请求的响应中.
当我在 Chrome 高级 REST 客户端中使用相同的 HTTP 方法 (GET /ReferentielInfoConso/offres-tarifaires/identifiants HTTP/1.1) 请求相同的 URL 时,我可以看到预期的 header "Access-Control-Allow-Origin" 我可以访问资源 :
那么我应该怎么做才能让它在我的 AngularJS 应用程序中工作(不创建 Node.js 后端,我需要保持它非常轻)?为什么在 WSO2 中启用 CORS 还不够?
编辑:如果我的 AngularJS 应用程序直接请求 API 到 Tomcat 7 端点(默认启用 CORS)而不通过 WSO2 AM,它可以完美运行。
编辑 2:当我的 AngularJS 应用程序试图在不发送授权令牌的情况下访问 WSO2 API 时,我收到 401 响应,包括预期的 "Access-Control-Allow-Origin" header .一旦我添加令牌,它看起来就像通过 WSO2 AM 遵循不同的路线,我没有得到 "Access-Control-Allow-Origin" header.
编辑 3:如果我有 "Autorization" header 并尝试直接访问我的 Tomcat 7 后端,我也会遇到 "cross-origin" 问题,但我想是因为 "Authorization" header 不在 Tomcat conf 允许的 header 列表中。它在 WSO2 AM 允许的 header 列表中(我添加了 "authorization" 和 "Authorization" 以确保它能正常工作)。
如果您没有在 API 发布者 UI 的资源列表中定义 OPTIONS 方法,APIM 应该发送所有 CORS headers,如 api-manager.xml.
另请注意,您可以根据发布者 UI 的 API 定义 CORS 配置。如果您进行了该配置,则 api-manager.xml 中给出的 CORS 配置将针对该特定 API 忽略。
如果您的后端是 CORS-enabled,您也可以向后端发送 OPTIONS 请求。为此,您可以在 API Publisher 中为 /* OPTIONS
.
创建一个资源
对于我的特殊情况,我希望客户端应用程序在 AJAX 中请求令牌并调用 API,我选择了最简单的解决方案:使用这些在与 WSO2 相同的机器上配置 Apache Web 服务器下面是简单的配置行。
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE,PATCH,OPTIONS"
Header set Access-Control-Allow-Headers "authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,Authorization"
ProxyPass "/" "http://10.22.106.101:8280/"
ProxyPassReverse "/" "http://10.22.106.101:8280/"
所以这是最终答案:WSO2 中存在多个错误,这些错误使得 CORS 无法直接从 Web 应用程序(无后端)与 APIs 交互。
首先,由于没有记录的原因,/token API 不受 repository/conf/api-manager.xml 中全局 api conf 的影响,因此您需要深入 conf (repository/deployment/server/synapse-configs/default/api/TokenAPI.xml) 并添加 CORSRequestHandler conf 从任何其他 xml 文件在同一目录中。
其次,当你使用默认版本时URL (http://host:port/Context/myapi instead of http://host:port/Context/1.0/myapi when the 1.0 version is marked as default version), you won't receive the CORS headers : https://wso2.org/jira/browse/APIMANAGER-5361
因此,在 TokenAPI.xml 中手动配置 CORShandler 并将我的调用替换为显式版本调用后,我可以删除我的反向代理。我仍然期待默认版本错误得到纠正,因为默认版本是一个有用的功能。
我通过修改 WSO2 repository\resources\api_templates 文件夹中包含的文件 default_api_template.xml 解决了这个错误 API经理分布.
我刚刚修改了行
<resource methods="POST PATCH GET DELETE HEAD PUT">
通过以这种方式添加 OPTIONS 动词:
<resource methods="POST PATCH GET DELETE HEAD PUT OPTIONS">*
现在默认端点也适用于 cors 调用。
当我的客户端 AngularJS 应用程序试图访问通过 WSO2 API 管理器 (v2.0.0) 公开的一些 REST API 时,我得到了这个响应(IP,端口替换为 "am_host" 和 "am_port") :
Refused to set unsafe header "Origin"
XMLHttpRequest cannot load http://am_host:am_port/ReferentielInfoConso/offres-tarifaires/identifiants. 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:8000' is therefore not allowed access.
当我检查 Chrome 开发者工具中的 headers 时,我可以确认 "Access-Control-Allow-Origin" header 不存在于对 "OPTIONS" 请求的响应中.
当我在 Chrome 高级 REST 客户端中使用相同的 HTTP 方法 (GET /ReferentielInfoConso/offres-tarifaires/identifiants HTTP/1.1) 请求相同的 URL 时,我可以看到预期的 header "Access-Control-Allow-Origin" 我可以访问资源 :
那么我应该怎么做才能让它在我的 AngularJS 应用程序中工作(不创建 Node.js 后端,我需要保持它非常轻)?为什么在 WSO2 中启用 CORS 还不够?
编辑:如果我的 AngularJS 应用程序直接请求 API 到 Tomcat 7 端点(默认启用 CORS)而不通过 WSO2 AM,它可以完美运行。
编辑 2:当我的 AngularJS 应用程序试图在不发送授权令牌的情况下访问 WSO2 API 时,我收到 401 响应,包括预期的 "Access-Control-Allow-Origin" header .一旦我添加令牌,它看起来就像通过 WSO2 AM 遵循不同的路线,我没有得到 "Access-Control-Allow-Origin" header.
编辑 3:如果我有 "Autorization" header 并尝试直接访问我的 Tomcat 7 后端,我也会遇到 "cross-origin" 问题,但我想是因为 "Authorization" header 不在 Tomcat conf 允许的 header 列表中。它在 WSO2 AM 允许的 header 列表中(我添加了 "authorization" 和 "Authorization" 以确保它能正常工作)。
如果您没有在 API 发布者 UI 的资源列表中定义 OPTIONS 方法,APIM 应该发送所有 CORS headers,如 api-manager.xml.
另请注意,您可以根据发布者 UI 的 API 定义 CORS 配置。如果您进行了该配置,则 api-manager.xml 中给出的 CORS 配置将针对该特定 API 忽略。
如果您的后端是 CORS-enabled,您也可以向后端发送 OPTIONS 请求。为此,您可以在 API Publisher 中为 /* OPTIONS
.
对于我的特殊情况,我希望客户端应用程序在 AJAX 中请求令牌并调用 API,我选择了最简单的解决方案:使用这些在与 WSO2 相同的机器上配置 Apache Web 服务器下面是简单的配置行。
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE,PATCH,OPTIONS"
Header set Access-Control-Allow-Headers "authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,Authorization"
ProxyPass "/" "http://10.22.106.101:8280/"
ProxyPassReverse "/" "http://10.22.106.101:8280/"
所以这是最终答案:WSO2 中存在多个错误,这些错误使得 CORS 无法直接从 Web 应用程序(无后端)与 APIs 交互。
首先,由于没有记录的原因,/token API 不受 repository/conf/api-manager.xml 中全局 api conf 的影响,因此您需要深入 conf (repository/deployment/server/synapse-configs/default/api/TokenAPI.xml) 并添加 CORSRequestHandler conf 从任何其他 xml 文件在同一目录中。
其次,当你使用默认版本时URL (http://host:port/Context/myapi instead of http://host:port/Context/1.0/myapi when the 1.0 version is marked as default version), you won't receive the CORS headers : https://wso2.org/jira/browse/APIMANAGER-5361
因此,在 TokenAPI.xml 中手动配置 CORShandler 并将我的调用替换为显式版本调用后,我可以删除我的反向代理。我仍然期待默认版本错误得到纠正,因为默认版本是一个有用的功能。
我通过修改 WSO2 repository\resources\api_templates 文件夹中包含的文件 default_api_template.xml 解决了这个错误 API经理分布.
我刚刚修改了行
<resource methods="POST PATCH GET DELETE HEAD PUT">
通过以这种方式添加 OPTIONS 动词:
<resource methods="POST PATCH GET DELETE HEAD PUT OPTIONS">*
现在默认端点也适用于 cors 调用。