我使用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"/>
我可以很高兴地通过http://localhost:8080/foo/2012-07-17/123
直接从Firefox/IE使用端点,但我正在尝试构建一个将从另一个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会更加复杂,因为Web服务是由NTLM servlet过滤器投影的。我一直在为非msie浏览器使用jQuery,以便使用xhrFields属性传递凭据,并且我可以看到我的服务在调试器中从IE&Firefox,但当从Firefox调用时,我的响应是空白的。
这可能吗?
Firefox似乎不尊重Access-Control-Allow-Origin: *
标头-更改@CrossOriginResourceSharing
注释以指定将访问端点的主机/端口组合列表,从而解决了此问题。