跨域访问Restful JAX-RS服务

4

我使用cxf创建了一个jax-rs restful服务,并注解了我的scala服务实现以暴露cors头:

@Path("/foo/{date}")
@Produces(Array("application/xml"))
@CrossOriginResourceSharing(allowAllOrigins = true)
class Foo {
    @GET
    @Path("{id}")
    def doStuff(@PathParam("date") date: util.Date, @PathParam("id") id: Int) = ...
}

在我的Spring applicationContext.xml文件中,我已经在jaxrs:providers列表中注册了一个cors过滤器。
<bean id="corsFilter" class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter"
        p:allowCredentials="true"/>

我可以直接通过Firefox/IE使用端点,地址为http://localhost:8080/foo/2012-07-17/123。但是我正在尝试构建一个服务,该服务将从另一个Web应用程序中调用,以分离这两个应用程序之间的联系。 当我直接通过Firefox发出请求时,我看到以下内容:
Response Headers
Content-Length                  5699
Content-Type                    application/xml
Date                            Wed, 18 Jul 2012 16:49:09 GMT
Server                          Apache-Coyote/1.1

Request Headers
Accept                          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding                 gzip, deflate
Accept-Language                 en-us,en;q=0.5
Cache-Control                   max-age=0
Connection                      keep-alive
Cookie                          DWRSESSIONID=Q62Vf$dv*S9sA8EaJm6jKW6$pyj; JSESSIONID=17E120C419F075B505447F151124BC18
Host                            localhost:9580
User-Agent                      Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1

当我通过本地磁盘上的网页发出Ajax请求时,我看到以下内容:

Response Headers
Access-Control-Allow-Cred...    true
Access-Control-Allow-Orig...    *
Content-Length                  6177
Content-Type                    application/xml
Date                            Wed, 18 Jul 2012 16:41:21 GMT
Server                          Apache-Coyote/1.1

Request Headers
Accept                          */*
Accept-Encoding                 gzip, deflate
Accept-Language                 en-us,en;q=0.5
Connection                      keep-alive
Cookie                          DWRSESSIONID=Q62Vf$dv*S9sA8EaJm6jKW6$pyj; JSESSIONID=17E120C419F075B505447F151124BC18
Host                            localhost:8080
Origin                          null
User-Agent                      Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1

我可以使用prototype.js在IE中很好地实现这一点,但是在Firefox中存在进一步的复杂性,我认为这是由于webservice由NTLM servlet过滤器投影所致。我正在使用jQuery来处理非IE浏览器,以便使用xhrFields属性传递凭据,并且我可以从IE和Firefox中的调试器中看到我的服务被调用,但是当从Firefox调用时我的响应为空。
这种情况是否可能发生?
1个回答

3

看起来Firefox不支持Access-Control-Allow-Origin: *头信息。将@CrossOriginResourceSharing注解更改为指定可以访问端点的主机/端口组合列表可以解决此问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接