[英]Cross domain problems with WSO2 API Manager
我们已经为客户开发了一些API,我们已经通过API Manager发布了它们。 我们已经为客户端提供了一些关于PHP的代码示例,它们运行良好。 唯一的问题是他们在与AM关联的域中通过AJAX使用这些API。 这是跨域问题吗?
我尝试使用以下标头在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: *
但即使使用这些标题,我仍然可以在拨打AM时获得401 Unknownizaed。 我已经尝试直接向AM发出请求而不通过Apache(端口8282),但我们仍然遇到同样的问题。
是的,这是一个跨域问题。 我建议你试试下面的内容。
您的API是否允许在“无认证类型”中使用“OPTIONS”动词? [1]验证是否向带有OAuth标头的API发送curl请求。 如果您使用CORS标题获得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
请将Access-Control-Allow-Headers值更改为授权,Access-Control-Allow-Origin,Content-Type和check。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.