0

我正在尝试配置 GWT 应用程序以使用不同的后端服务器(运行 Tomcat 7.0.26)。

我按照该网站的说明配置了 CORS 过滤器:http: //software.dzhuvinov.com/cors-filter-installation.html

我使用了示例 GWT 代码(Greetings App),并进行了以下修改:

  1. 在 GWT App 中,我在 RPC 调用之前添加了这一行:

    ((ServiceDefTarget) greetingService).setServiceEntryPoint("http://remoteserver/testcors/greet");
    
  2. 我修改了 web.xml 并添加了 CORS 过滤器:

    <filter>
            <!-- The CORS filter with parameters -->
            <filter-name>CORS</filter-name>
            <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    
            <!-- Note: All parameters are options, if ommitted CORS Filter will fall 
                    back to the respective default values. -->
            <init-param>
                    <param-name>cors.allowGenericHttpRequests</param-name>
                    <param-value>true</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.allowOrigin</param-name>
                    <param-value>*</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.allowSubdomains</param-name>
                    <param-value>false</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.supportedMethods</param-name>
                    <param-value>GET, HEAD, POST, OPTIONS</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.supportedHeaders</param-name>
                    <param-value>Content-Type, X-Requested-With</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.exposedHeaders</param-name>
                    <param-value>X-Test-1, X-Test-2</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.supportsCredentials</param-name>
                    <param-value>true</param-value>
            </init-param>
    
            <init-param>
                    <param-name>cors.maxAge</param-name>
                    <param-value>3600</param-value>
            </init-param>
    
    </filter>
    
    <filter-mapping>
            <!-- CORS Filter mapping -->
            <filter-name>CORS</filter-name>
            <url-pattern>/testcors/greet</url-pattern>
    </filter-mapping>
    

如果我从“远程服务器”访问 TestCORS 应用程序,一切正常,我可以看到添加了这些标头:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://remoteserver
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: X-Test-2, X-Test-1
Content-Encoding: gzip
Content-Disposition: attachment
Content-Type: application/json;charset=utf-8
Content-Length: 214
Date: Sun, 24 Feb 2013 15:12:42 GMT

但是,如果我访问在本地机器(127.0.0.1:8888)上运行的同一个 TestCORS GWT 应用程序,我会收到以下错误:

请求标头:

OPTIONS /testcors/greet HTTP/1.1
Host: remoteserver
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://127.0.0.1:8888
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
Access-Control-Request-Headers: x-gwt-module-base, x-gwt-permutation, origin, content-type
Accept: */*
Referer: http://127.0.0.1:8888/TestCORS.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

响应标头:

HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 96
Date: Sun, 24 Feb 2013 15:14:38 GMT

我尝试了不同的浏览器(Chrome 版本 24.0.1312.57、Safari 版本 6.0.2 (8536.26.17)、Firefox 18.0.2、Opera 版本 12.14),我得到了服务器返回的相同错误 - HTTP 403。

我看到很少有人遇到类似的问题,但我无法找到解决方案:

非常感谢任何帮助!

4

2 回答 2

1

我以前没有使用过这个 cors-filter,我更喜欢这样一个最简单的。

无论如何,在您的情况下,您似乎没有正确配置标题 gwt 需要工作,您的应用程序正在发送:

Access-Control-Request-Headers: x-gwt-module-base, x-gwt-permutation, origin, content-type

所以,你的配置应该有:

<init-param>
   <param-name>cors.supportedHeaders</param-name>
   <param-value>x-gwt-module-base, x-gwt-permutation, origin, content-type</param-value>
</init-param>
于 2013-02-25T07:52:48.737 回答
0

感谢 Vladimir,我还从这个网站获得了 CORS 过滤器:http: //software.dzhuvinov.com/cors-filter-installation.html

我在 web.xml 中添加了其他支持的标头:

<init-param>
    <param-name>cors.supportedHeaders</param-name>
    <param-value>Content-Type, X-Requested-With, x-gwt-module-base, x-gwt-permutation, origin, content-type</param-value>
</init-param>
于 2013-02-26T01:03:18.673 回答