Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CORS Filter Tomcat 7 Not Working

I'm trying to implement the basic CORS filter in the $CATALINA_BASE/conf/web.xml file. Here's my filter:

<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>

I've confirmed that I'm definitely running a supported version of Tomcat:

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

Here's my AJAX request:

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

And my request/response headers:

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

Any help would be appreciated. I have a feeling I'm not sending the request in the proper manner but really have no idea.

like image 364
Patrick Dwyer Avatar asked Jan 29 '15 15:01

Patrick Dwyer


1 Answers

If your web.xml has other filters with dispatcher specifics like

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

Then CORS Filter didn't kick in for me.

However following filter-mapping did the trick.

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

<dispatcher>FORWARD</dispatcher> was the magic

Hope this helps you friend!

like image 163
suryakrupa Avatar answered Nov 10 '22 16:11

suryakrupa