Cross-Origin(Camel-route和angularjs)

时间:2018-05-04 15:19:04

标签: cors apache-camel

您好我正在尝试使用apache camel来提供angularjs,但是有错误:  No' Access-Control-Allow-Origin'如果我使用restConfiguration,则标头出现在请求的资源上。出了点问题

在我的蓝图中,我启用了CROS并设置了这样的属性标题,但不能正常工作:

<restConfiguration bindingMode="json" component="jetty" enableCORS="true">
    <dataFormatProperty key="prettyPrint" value="true" />
    <corsHeaders key="Access-Control-Allow-Methods" value="GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH"></corsHeaders>
    <corsHeaders key="Access-Control-Allow-Origin" value="*"></corsHeaders>
    <corsHeaders key="Access-Control-Allow-Headers" value="Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"></corsHeaders>
    <corsHeaders key="Access-Control-Max-Age" value="3600"></corsHeaders>
</restConfiguration>

如果我删除其余配置并设置如下所示的标题,我就不会有错误。

 <route id="jetty" streamCache="true">
        <from uri="jetty:http://0.0.0.0:9100/ifom/miki?matchOnUriPrefix=true"/>
        <to uri="jetty:http://localhost:8080/om/miki?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
        <log message="jetty-done header: ${headers}"/>
        <setHeader headerName="Access-Control-Allow-Headers"><constant>X-Auth-Token, Content-Type</constant></setHeader>
        <setHeader headerName="Access-Control-Allow-Origin"><constant>*</constant></setHeader>
        <setHeader headerName="Access-Control-Allow-Methods"><constant>OPTIONS, POST</constant></setHeader>
    </route>

为什么restConfiguration不起作用? 感谢&#39; S

1 个答案:

答案 0 :(得分:0)

我最终通过在Camel和AngularJs上为Access-Control-Allow-Headers和Access-Control-Request-Header设置相同的值来解决了这个问题。

AngularJs:

headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Request-Headers': 'access-control-allow-methods,access-control-allow-origin,authorization,content-type',
'Access-Control-Allow-Headers': 'access-control-allow-methods,access-control-allow-origin,authorization,content-type',
'Access-Control-Allow-Methods': 'GET, DELETE, POST, OPTIONS, PUT',
}

骆驼:

.setHeader("Access-Control-Allow-Origin", constant("*"))        
.setHeader("Access-Control-Allow-Headers", constant("access-control-allow-methods,access-control-allow-origin,authorization,content-type"))        
.setHeader("Access-Control-Allow-Methods", constant("GET, DELETE, POST, OPTIONS, PUT"))