如何在jBoss中启用CORS

use*_*888 1 jquery html5 jboss soap web-services

我正在开发一个HTML5应用程序,该应用程序必须从旧版Web服务(Jax-Ws)中获取一些值,因此我使用jQuery.soap来查询这些Web服务以获得响应。我已经使用SOAP UI尝试了请求的正确性,并且它们可以正常工作。

从我的HTML5客户端,我无法从服务器接收SOAP响应,因为在响应中,没有将Allow-Control-Allow-Origin标头设置为*。因此,请求的来源被识别为不允许,并且服务器的响应是错误响应。

使用Firebug + Firefox调试我的HTML5项目时,错误消息为:

跨源请求已锁定:源处的条件不允许读取远程资源。您可以通过将资源移至同一域或激活CORS来解决问题。

如何在jBoss中启用CORS?

小智 5

您需要处理旧版Web服务才能解决此问题。正如上面的mccannf所说,您需要在web.xml中添加CORS过滤器。

您可以使用来自交易公司的解决方案:

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>
Run Code Online (Sandbox Code Playgroud)

专家

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果您使用apache Tomcat,则可以使用内置的CorsFilter:

web.xml:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

pom.xml:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.42</version>
    <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)