我们尝试从 Angular 5 应用程序调用 REST API,这些 API 被设计并部署在 EI 上作为 .car 文件,但是,当我们启动 Angular 应用程序时,由于No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.5.222.34:9763' is therefore not allowed access.
. 我们尝试添加 CORS 过滤器,<EI_HOME>/conf/tomcat/web.xml
但没有成功:
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
谁能帮我找出问题所在以及如何解决?并提前感谢您。
我要求的代码是:
从角度来看是一个普通的 HTTP POST 客户端调用,但是,浏览器发送两个请求,第一个作为 OPTIONS 来检查服务器是否会接受原始请求(POST 请求),第二个是原来的 POST 。
从EI方面:
<api context="/cmbassadmin/authentication" name="authentication" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="OPTIONS POST" uri-template="/login">
<inSequence>
<sequence key="InitiateCMBASSADMINAuthenticationSQ"/>
<header name="Access-Control-Allow-Origin" scope="transport" value="*"/>
<header name="Access-Control-Allow-Methods" scope="transport" value="GET,PUT,POST,DELETE,PATCH,OPTIONS"/>
<header name="Access-Control-Allow-Headers" scope="transport" value="*"/>
<header name="Content-Type" scope="transport" value="text/html;charset=utf-8"/>
<property name="Access-Control-Allow-Origin" scope="transport" type="STRING" value="*"/>
<property name="Access-Control-Allow-Methods" scope="transport" type="STRING" value="GET,PUT,POST,DELETE,PATCH,OPTIONS"/>
<property name="Access-Control-Allow-Headers" scope="transport" type="STRING" value="*"/>
<filter regex="OPTIONS" source="get-property('REST_METHOD')">
<then>
<log>
<property name="message" value="in the preflight"/>
</log>
<respond/>
</then>
<else/>
</filter>
<log>
<property expression="get-property('uri.var.username')" name="uri.var.username"/>
</log>
<call-template target="audit_trail">
<with-param name="user_name" value="{get-property('uri.var.username')}"/>
<with-param name="action_name" value="Login"/>
<with-param name="description" value="Login to the xyz admin app"/>
</call-template>
<sequence key="AuthenticateWithIS_SQ"/>
<loopback/>
</inSequence>
<outSequence>
<header name="Access-Control-Allow-Origin" scope="transport" value="*"/>
<header name="Access-Control-Allow-Methods" scope="transport" value="GET,PUT,POST,DELETE,PATCH,OPTIONS"/>
<header name="Access-Control-Allow-Headers" scope="transport" value="*"/>
<header name="Content-Type" scope="transport" value="text/html;charset=utf-8"/>
<property name="Access-Control-Allow-Origin" scope="transport" type="STRING" value="*"/>
<property name="Access-Control-Allow-Methods" scope="transport" type="STRING" value="GET,PUT,POST,DELETE,PATCH,OPTIONS"/>
<property name="Access-Control-Allow-Headers" scope="transport" type="STRING" value="*"/>
<send/>
</outSequence>
<faultSequence>
<send/>
</faultSequence>
</resource>
</api>