【问题标题】:CORS Filter Tomcat 7 Not WorkingCORS过滤器Tomcat 7不工作
【发布时间】:2015-03-28 21:32:15
【问题描述】:

我正在尝试在 $CATALINA_BASE/conf/web.xml 文件中实现基本的 CORS 过滤器。这是我的过滤器:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

我已经确认我运行的是受支持的 Tomcat 版本:

Server version: Apache Tomcat/7.0.56
Server built:   Sep 26 2014 12:08:24
Server number:  7.0.56.0

这是我的 AJAX 请求:

function MethodOne() {
$.ajax({
    type: "GET",
    url: "http://localhost:8080/crossDomain",
    crossDomain: true,
    success: function(response) {
        $('#result').html(response);
    }   
});
}

还有我的请求/响应标头:

Remote Address:[::1]:8080
Request URL:http://localhost:8080/crossDomain
Request Method:GET
Status Code:302 Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36    (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
Response Headersview source
Date:Thu, 29 Jan 2015 15:19:00 GMT
Location:http://localhost:8080/crossDomain/
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

任何帮助将不胜感激。我感觉我没有以正确的方式发送请求,但我真的不知道。

【问题讨论】:

  • 我有一个非常相似的问题,但我的失败,在预检 OPTIONS 请求中出现 403。

标签: ajax tomcat tomcat7 cors


【解决方案1】:

我遇到了同样的问题。不得不取消过滤器。添加了

request.getHeader("Origin");

在响应标头中。这为我解决了问题。

另一个解决方案是检查它是否是预检请求。然后不得不补充:

Request.ok().build();

但我想最好的解决方案是在浏览器中禁用网络安全。

对于我使用的 Chrome:

--disable-web-security 

【讨论】:

    【解决方案2】:

    如果您的web.xml 有其他过滤器,例如

     <dispatcher>REQUEST</dispatcher>
     <dispatcher>INCLUDE</dispatcher>
     <dispatcher>FORWARD</dispatcher>
    

    然后我没有使用 CORS 过滤器。

    但是,过滤器映射成功了。

    <filter-mapping>
        <filter-name>CORS Filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    

    &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt; 是魔法

    希望这对你有帮助!

    【讨论】:

    • 它解决了我的问题,但它没有维护我的会话有什么办法可以让我也可以维护我的会话
    猜你喜欢
    • 2013-06-20
    • 2014-08-14
    • 2018-02-25
    • 2015-10-13
    • 1970-01-01
    • 2018-10-22
    • 2014-05-15
    • 2014-05-03
    • 2019-01-12
    相关资源
    最近更新 更多