如何在不使用API 管理器的情况下在Mule 4中配置CORS?

问题描述 投票:0回答:2

我们正在尝试从angular应用程序调用the子服务,并收到CORS错误。

当我们在global.xml的http config中添加CORS拦截器时,我们的项目遇到了问题。

<http:listener-config name="api-httpListenerConfig">
    <http:listener-connection host="${http.host}" port="${http.private.port}" />
<http:listener-interceptors>
        <http:cors-interceptor allowCredentials="true">
            <http:origins>
                <http:origin url="*" accessControlMaxAge="0">
                    <http:allowed-methods>
                        <http:method methodName="GET" />
                        <http:method methodName="POST" />
                        <http:method methodName="DELETE" />
                        <http:method methodName="OPTIONS" />
                        <http:method methodName="HEAD" />
                        <http:method methodName="PUT" />
                    </http:allowed-methods>
                     <http:allowed-headers>
                        <http:header headerName="X-Allow-Origin" />
                    </http:allowed-headers>
                </http:origin>
            </http:origins>
        </http:cors-interceptor>
    </http:listener-interceptors> 
</http:listener-config>

GET服务不应被浏览器中的CORS策略阻止。

mule mule-studio anypoint-studio mulesoft
2个回答
2
投票

API管理器使您可以将网关逻辑(CORS协商,身份验证/授权,流量管理等)保留在Mule应用程序之外。但是,如果这是一个内部应用程序,并且不能真正从API网关中受益,则这是在Mule应用程序中处理CORS的一种方法:

  1. API中的实施选项方法。这将必须在另一个来源将调用或尝试的所有资源上独立完成(不确定是否可能,尤其是在使用API​​KitRouter的情况下),然后在正则表达式模式下实现选项以处理所有资源。
  2. 返回侦听器上的Access-Control-Allow-Origin标头以及尝试访问该请求的源。还添加具有适当值的适当的Access-Control-Allow-Methods和Access-Control-Allow-Origins标头。

拦截器可能是出于相同的目的,但是尝试不起作用。


0
投票

您能否共享用于返回标头的示例代码。

谢谢,Vish

© www.soinside.com 2019 - 2024. All rights reserved.