产地http://127.0.0.1:8888没有被访问控制允许来源允许(Origin http:/

2019-08-06 06:38发布

而尝试使用GWT客户端来访问REST Web服务我得到了这个问题。 我检查了Chrome浏览器网页,然后我得到了来自控制台下面的错误

XMLHttpRequest cannot load http://localhost:8080/RestWeb/webresources/generic/get. Origin http://127.0.0.1:8888 is not allowed by Access-Control-Allow-Origin.

以下是我的客户端代码

public void onModuleLoad() {

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            "http://localhost:8080/RestWeb/webresources/generic/get");

    builder.setCallback(new RequestCallback() {

        @Override
        public void onResponseReceived(Request request, Response response) {

            Window.alert("onResponseReceived");

        }

        @Override
        public void onError(Request request, Throwable exception) {

        }
    });
    builder.setHeader("Content-Type",
            "text/plain,application/json,text/xml");

    builder.setHeader("Access-Control-Allow-Methods",
            "PUT, GET, POST, DELETE, OPTIONS");
    builder.setHeader("Access-Control-Allow-Headers", "Content-Type");
    builder.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8888");
try {
            builder.send();
        } catch (RequestException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

我的服务器端代码:

@Path("generic")
@WebService
public class GenericResource {

@Context
private UriInfo context;
@Context
private HttpServletResponse response;
private String content = "content";

/**
 * Creates a new instance of GenericResource
 */
public GenericResource() {
}

@GET

@Path("/get")
@Produces("application/json,text/plain")
public String getXml() {
    System.out.println("GET");
    //response.addHeader(content, content);

    return this.content + " from get method";

}
}

我尝试用不同的方式来获得答案。 请帮我。

Answer 1:

你需要改变你的服务器代码支持CORS。

一种选择是一个过滤器:

public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            // CORS "pre-flight" request
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Access-Control-Max-Age", "1800");//30 min
        }
        filterChain.doFilter(request, response);
    }
}

在web.xml中需要加入过多如下:

  <filter>
    <filter-name>cors</filter-name>
    <filter-class>com.xxx.CorsFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>cors</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


Answer 2:

由于这是一个老问题,如果今天所面临类似的问题 - 他或她可能要考虑使用一个漂亮的“CORS过滤”处理所有CORS的东西你在一个完全透明的方式。 这里的链接



文章来源: Origin http://127.0.0.1:8888 is not allowed by Access-Control-Allow-Origin
标签: rest gwt