CORS Filter Tomcat 7 not working

I am trying to implement a basic CORS filter in the file $ CATALINA_BASE / conf / web.xml. Here is 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 have confirmed that I definitely support the 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 is 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 greatly appreciated. I have a feeling that I am not sending the request properly, but I really have no idea.

+6
source share
2 answers

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

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

Then the CORS filter didn't hit me.

However, after filtering the filter, the trick.

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

<dispatcher>FORWARD</dispatcher> was magical

Hope this helps you friend!

+1
source

I had the same problem. I had to end the filter. Added

 request.getHeader("Origin"); 

in the response header. This solved the problem for me.

Another solution is to check if this is a pre-sale request. Then it was necessary to add:

 Request.ok().build(); 

But I think the best solution was to disable web security in the browser.

For Chrome, I used:

 --disable-web-security 
0
source

Source: https://habr.com/ru/post/981715/


All Articles