RESTful Web 服务:如何在 Java 中设置标头以接受 Access-Control-Allow-Origin 允许的 XMLHttpRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18234366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:36:11  来源:igfitidea点击:

RESTful webservice : how to set headers in java to accept XMLHttpRequest allowed by Access-Control-Allow-Origin

javarestjax-wsjax-rscors

提问by ChiranjeeviIT

I have a RESTful webservice which will return string and it was written in Java (JAX-WS). My problem is when I send request to that webservice with URL like :

我有一个 RESTful web 服务,它将返回字符串,它是用 Java (JAX-WS) 编写的。我的问题是当我使用如下 URL 向该网络服务发送请求时:

http://localhost:8080/project/webservices/getlist/getListCustomers

http://localhost:8080/project/webservices/getlist/getListCustomers

In the console it's giving me the error message below:

在控制台中,它给了我以下错误消息:

XMLHttpRequest cannot load url Origin localhost is not allowed by Access-Control-Allow-Origin

XMLHttpRequest 无法加载 url Origin localhost is not allowed by Access-Control-Allow-Origin

How can I handle this issue?

我该如何处理这个问题?

Java code:

爪哇代码:

@GET
@Path("/getsample")
public Response getMsg() { 
    String output = "Jersey say : " ;   
    return Response.status(200).entity(output).build();
}

采纳答案by flyer

Read here about your issue CORS : http://enable-cors.org/

在此处阅读有关您的 CORS 问题:http: //enable-cors.org/

Check if this one help you in your getMsg() method:
return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();

检查这是否对您的 getMsg() 方法有帮助:
return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();

If above doesn't work try to add Jersey filter to your service. Create filter class:

如果上述方法不起作用,请尝试将 Jersey 过滤器添加到您的服务中。创建过滤器类:

package your.package;

public class CORSFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {

        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
        cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");

        return cresp;
    }
}

And register later win web.xml with:

并稍后注册 win web.xml:

<servlet>
<servlet-name>CORS Filter</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>your.package.CORSFilter</param-value>
 </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>CORS Filter</servlet-name>
    <url-pattern>/webservices/*</url-pattern>
</servlet-mapping>



另一种解决方案是在您的资源中使用此代码OPTIONSOPTIONS为浏览器提供。把它放在你有@GET 的班级里。

  @OPTIONS
  @Path("/getsample")
  public Response getOptions() {
    return Response.ok()
      .header("Access-Control-Allow-Origin", "*")
      .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
      .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With").build();
  }



如果这不起作用,请尝试将"*""*"提供的“Access-Control-Allow-Origin”标头与您访问此资源的自定义域交换。Ig 如果你从http://localhost::8080http://localhost::8080使用类似的东西("Access-Control-Allow-Origin", "http://localhost:8080")("Access-Control-Allow-Origin", "http://localhost:8080")而不是 asterisk调用它"*""*"