gpt4 book ai didi

cross-domain - WSO2 API Manager 的跨域问题

转载 作者:行者123 更新时间:2023-12-04 04:53:23 27 4
gpt4 key购买 nike

我们为客户开发了一些 API,并通过 API Manager 发布了它们。我们为客户提供了一些关于 PHP 的代码示例,这些示例运行良好。唯一的问题是他们通过 AJAX 在与 AM 关联的域不同的域中使用这些 API。这是一个跨域问题吗?

我尝试使用以下 header 在 API Manager 前面设置 apache 服务器,以便允许跨域

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization, Content-Type, Accept
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Access-Control-Allow-Origin: *

但即使有这些 header ,我在调用 AM 时仍然会收到 401 Unauthorizaed。我尝试不通过 Apache(端口 8282)直接向 AM 发出请求,但我们仍然遇到同样的问题。

最佳答案

是的,这是一个跨域问题。我建议你在下面试试。

None Auth Type 中的“OPTIONS”动词是否允许您的 API? [1] 验证向 API 发送不带 OAuth header 的 curl 请求。如果您收到带有您提到的 CORS header 的 200 OK 响应,那应该没问题。
前任:

curl -v -X OPTIONS http://localhost:8280/testapi

如果它没有返回成功消息,那么您的后端可能不支持 OPTIONS 方法。您可以通过直接向后端服务发送 OPTIONS 请求来验证这一点。您可以在后端服务中启用 OPTIONS,也可以通过修改 API 突触配置来避免 OPTIONS 调用到达后端。

前任:-
<api name="admin--TestAPI" context="/test" version="1.0" version-type="url">
<resource methods="POST GET OPTIONS DELETE PUT" url-mapping="/*">
<inSequence>
<filter source="get-property('axis2', 'HTTP_METHOD')" regex="OPTIONS">
<then>
<log level="custom">
<property name="Message" value="Received OPTIONS call, sending back headers"/>
</log>
<property name="Access-Control-Request-Headers" value="authorization,content-type" scope="transport"/>
<property name="Access-Control-Allow-Headers" value="authorization,Access-Control-Allow-Origin,Content-Type" scope="transport"/>
<property name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" scope="transport"/>
<property name="Access-Control-Allow-Origin" value="*" scope="transport"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<header name="To" action="remove"/>
<send/>
</then>
<else>
<property name="POST_TO_URI" value="true" scope="axis2"/>
<filter source="$ctx:AM_KEY_TYPE" regex="PRODUCTION">
<then>
<send>
<endpoint name="admin--StudentAPI_APIEndpoint_0">
<address uri="http://localhost:8080/sample/1.0/one/">
<timeout>
<duration>30000</duration>
<responseAction>fault</responseAction>
</timeout>
<suspendOnFailure>
<errorCodes>-1</errorCodes>
<initialDuration>0</initialDuration>
<progressionFactor>1.0</progressionFactor>
<maximumDuration>0</maximumDuration>
</suspendOnFailure>
<markForSuspension>
<errorCodes>-1</errorCodes>
</markForSuspension>
</address>
</endpoint>
</send>
</then>
<else>
<sequence key="_sandbox_key_error_"/>
</else>
</filter>
</else>
</filter>
</inSequence>
<outSequence>
<send/>
</outSequence>
</resource>
<handlers>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.security.APIAuthenticationHandler"/>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler">
<property name="id" value="A"/>
<property name="policyKey" value="gov:/apimgt/applicationdata/tiers.xml"/>
</handler>
<handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtUsageHandler"/>
<handler class="org.wso2.carbon.apimgt.usage.publisher.APIMgtGoogleAnalyticsTrackingHandler"/>
<handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/>
</handlers>
</api>

然后将 Access-Control-Allow-Origin 也添加到 Access-Control-Allow-Headers 列表中,并保持其他标题不变。

例如: Access-Control-Allow-Headers: authorization,Access-Control-Allow-Origin,Content-Type
如果您仍然收到错误,您能否提供详细的错误消息或示例 PHP 客户端代码?

[1] http://docs.wso2.org/display/AM160/Adding+Documentation+Using+Swagger

关于cross-domain - WSO2 API Manager 的跨域问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21304290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com