【问题标题】:Mule 3.7 Apikit CORS - Access-Control-Allow-OriginMule 3.7 Apikit CORS - 访问控制允许来源
【发布时间】:2016-05-11 18:57:15
【问题描述】:

我正在使用带有 APIKit + RAML 的 Mule 3.7。缺乏关于 mule 的 http 响应构建器等的文档。

Http Credentials 不适用于 Access-Control-Allow-Origin 的通配符,因此需要删除通配符,并将值动态设置为消息头中的来源。

错误: 当凭证标志为真时,不能在“Access-Control-Allow-Origin”标头中使用通配符“*”。 Origin 'http://localhost:8080' 因此不允许访问。

如何将 Access-Control-Allow-Origin 值设置为 #[message.inboundProperties['origin']]

<flow name="api-main">
    <http:listener config-ref="api-httpListenerConfig" path="/api/*" doc:name="HTTP">
        <http:response-builder>
            <http:header headerName="Access-Control-Allow-Origin" value="*"/>
        </http:response-builder>
    </http:listener>
    <apikit:router config-ref="api-main-config" doc:name="APIkit Router"/>
    <exception-strategy ref="component-registry-apiKitGlobalExceptionMapping" doc:name="Reference Exception Strategy"/>
</flow>

【问题讨论】:

  • 您好,抱歉,我不确定您的要求。您想在对 API 的 http 请求中覆盖标头 Access-Control-Allow-Origin 以覆盖客户端发送的内容,或者您​​希望在响应客户端时覆盖标头,以便在响应中?
  • 在响应客户端时覆盖标题。
  • 这个问题应该不是mule版本特有的,好像问题和解决方案可以适用于新版本

标签: java cors mule


【解决方案1】:

这个问题不仅仅是在响应标头中添加标头。

如果您将允许凭据标头设置为 true,实际浏览器将使用 OPTION 方法发送预检请求以“测试”并允许源跨域请求。参考: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

仅仅设置一个 Access-Control-Allow-Origin 响应头是行不通的。

<set-property propertyName="Access-Control-Allow-Origin" value="*">

幸运的是,有一个 mule 模块来处理 CORS 请求,但它缺乏文档。您需要在 app.xml 的开头设置此配置。

<cors:config name="Cors_Configuration" doc:name="Cors Configuration">
    <cors:origins>
        <cors:origin url="http://localhost:8080">
            <cors:methods>
                <cors:method>POST</cors:method>
                <cors:method>DELETE</cors:method>
                <cors:method>PUT</cors:method>
                <cors:method>GET</cors:method>
            </cors:methods>
            <cors:headers>
                <cors:header>content-type</cors:header>
            </cors:headers>
        </cors:origin>
    </cors:origins>
</cors:config>

并在 APIkit 路由器之前使用它。

<http:inbound-endpoint address="http://localhost:8081/api" doc:name="HTTP" exchange-pattern="request-response"/>
<cors:validate config-ref="Cors_Configuration" publicResource="false" acceptsCredentials="true" doc:name="CORS Validate”/>
<apikit:router config-ref=“apiConfig" doc:name="APIkit Router”/>

注意:实际上你需要手动下载 mule-cors.xsd 定义文件,并复制到 /resources 文件夹。将此 xmlns 和 xsi 附加到您的 app.xml

<mule xmlns:cors="http://www.mulesoft.org/schema/mule/cors"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/cors classpath:/mule-cors.xsd" >

mule-cors.xsd: https://github.com/mulesoft/mule-module-cors/blob/master/src/main/resources/META-INF/mule-cors.xsd

参考:https://github.com/mulesoft/apikit/issues/27

【讨论】:

    【解决方案2】:

    只需在apikit路由器后使用set属性组件添加header即可。

    <set-property propertyName="Access-Control-Allow-Origin" value="*">
    

    响应时的 HTTP 连接器将转换标头中的所有出站属性。

    【讨论】:

      猜你喜欢
      • 2019-02-09
      • 2016-06-02
      • 2021-09-07
      • 2016-06-27
      • 2016-12-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 2020-11-26
      相关资源
      最近更新 更多