问题描述
我开发了一套安静的网络服务。我无法从远程客户端调用任何这些方法,因为错误
在所请求的资源上没有Access-Control-Allow-Origin头。
服务在localhost上正常工作。要在服务器端上执行任何更改或配置以解决此问题。
我使用WildFly 8,JavaEE 7
/ div>我想知道同样的事情,所以经过一番研究,我发现最简单的方法是简单地使用JAX-RS ContainerResponseFilter
添加相关CORS头。这样你不需要用CXF替换整个Web服务栈(Wildfly使用CXF是一些形式,但它看起来不像它使用它为JAX-RS也许只有JAX-WS)。
无论如何使用此过滤器,它都会将头部添加到每个REST Web服务中。
package com.yourdomain.package;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(final ContainerRequestContext requestContext,
final ContainerResponseContext cres) throws IOException {
cres.getHeaders()。add(Access-Control-Allow-Origin,*);
cres.getHeaders()。add(Access-Control-Allow-Headers,origin,content-type,accept,authorization);
cres.getHeaders()。add(Access-Control-Allow-Credentials,true);
cres.getHeaders()。add(Access-Control-Allow-Methods,GET,POST,PUT,DELETE,OPTIONS,HEAD);
cres.getHeaders()。add(Access-Control-Max-Age,1209600);然后当我使用curl测试的时候,我们可以看到,响应有CORS头:$ curl -D - http:// localhost: 8080 / rest / test
HTTP / 1.1 200 OK
X-Powered-By:Undertow 1
Access-Control-Allow-Headers:origin,content-type,accept,authorization
服务器:Wildfly 8
日期:Tue,2014年5月13日12:30:00 GMT
连接:keep-alive
访问控制允许原产地:*
访问-Control-Allow-Credentials:true
Transfer-Encoding:chunked
Content-Type:application / json
访问控制 - 最大年龄:1209600
访问控制允许-Methods:GET,POST,PUT,DELETE,OPTIONS,HEAD
我的理解是, code> @Provider
注释,告诉JAX-RS运行时使用过滤器,没有注释没有任何反应。
关于使用。
I developed a set of restful web services. I couldn't call any of these methods from remote clients due to the error
No 'Access-Control-Allow-Origin' header is present on the requested resource.
The services work perfectly on localhost. Is there any changes or configs to do on the server side to resolve the issue. i.e. to enable cross domain requests.
I'm using WildFly 8, JavaEE 7
解决方案I was wondering the same thing, so after a bit of research I found that the easiest way was simply to use a JAX-RS
ContainerResponseFilter
to add the relevant CORS headers. This way you don't need to replace the whole web services stack with CXF (Wildfly uses CXF is some form, but it doesn't look like it uses it for JAX-RS maybe only JAX-WS).Regardless if you use this filter it will add the headers to every REST webservice.
package com.yourdomain.package; import java.io.IOException; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.ext.Provider; @Provider public class CORSFilter implements ContainerResponseFilter { @Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext cres) throws IOException { cres.getHeaders().add("Access-Control-Allow-Origin", "*"); cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); cres.getHeaders().add("Access-Control-Allow-Credentials", "true"); cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); cres.getHeaders().add("Access-Control-Max-Age", "1209600"); } }
Then when I tested with curl, the response had the CORS headers:
$ curl -D - "http://localhost:8080/rest/test" HTTP/1.1 200 OK X-Powered-By: Undertow 1 Access-Control-Allow-Headers: origin, content-type, accept, authorization Server: Wildfly 8 Date: Tue, 13 May 2014 12:30:00 GMT Connection: keep-alive Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true Transfer-Encoding: chunked Content-Type: application/json Access-Control-Max-Age: 1209600 Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
My understanding is that it's the
@Provider
annotation that tells the JAX-RS runtime to use the filter, without the annotation nothing happens.I got the idea about using the
ContainerResponseFilter
from a Jersey example.这篇关于如何在JAX-RS网络服务上启用跨域请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!