GWT 中部署在两个不同域中的应用程序之间的交互

Interaction between application deployed in two different domain in GWT

我有一个应用程序,其中包含创建为 WAR(GWT 应用程序)的 UI 代码和部署在两个不同域中的 EAR 服务器端代码(8080,9090) 分别。从 80809090 通信时,它发送请求,甚至我可以看到它返回响应 200。但是客户端抛出一些 GWT 异常 **Permission denied to access 属性 'document' **。下图显示了 fire bug 中抛出的异常。

注意:我在服务器 9090 中启用了 CORS,请参阅服务器端代码 EAR 中添加的以下代码

@Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {

        HttpServletRequest request=(HttpServletRequest) arg0;
        HttpServletResponse response=(HttpServletResponse) arg1;
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        arg2.doFilter(request, response);
    }

我是否认为我需要在 GWT 客户端中添加 UI 来处理此 exception.Any 建议?

看来,您正试图对不同的 domain/port 发出 AJAX GWT-RPC 请求?即使你尝试 "cheat" 它,它也不会在所有浏览器中工作(它不会跨浏览器),因为 CORS 是 HTML5 附带的相当新的解决方案。 也许您应该考虑使用 REST 协议在客户端和服务器之间进行通信? 当我不得不在从 UI 到服务器的不同域上下文之间进行通信时,我遇到了这样的问题,唯一的方法是使用 REST 服务。跨浏览器解决方案。

如果您仍然想坚持打破 SOP 并应用 CORS,请尝试根据这篇文章更改您的过滤器:https://code.google.com/archive/p/gwtquery/wikis/Ajax.wiki

像这样:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;

String origin = req.getHeader("Origin");
if (origin != null && origin.matches(ALLOWED_DOMAINS_REGEXP)) {
  resp.addHeader("Access-Control-Allow-Origin", origin);
  if ("options".equalsIgnoreCase(req.getMethod())) {
    resp.setHeader("Allow", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");
    if (origin != null) {
      String headers = req.getHeader("Access-Control-Request-Headers");
      String method = req.getHeader("Access-Control-Request-Method");
      resp.addHeader("Access-Control-Allow-Methods", method);
      resp.addHeader("Access-Control-Allow-Headers", headers);
      // optional, only needed if you want to allow cookies.
      resp.addHeader("Access-Control-Allow-Credentials", "true");
      resp.setContentType("text/plain");
    }
    resp.getWriter().flush();
    return;
  }
}

// Fix ios6 caching post requests
if ("post".equalsIgnoreCase(req.getMethod())) {
  resp.addHeader("Cache-Control", "no-cache");
}

if (filterChain != null) {
  filterChain.doFilter(req, resp);
}

}

并且不要忘记正确配置过滤器:

    <filter> 
 <filter-name>CORSFilter</filter-name> 
<filter-class>my.namespace.CORSFilter</filter-class> 
</filter> 

<filter-mapping> 
<filter-name>CORSFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping>