简体   繁体   中英

CORS on Tomcat 6

I'm trying to enable CORS on Tomcat 6. Has anyone had success doing that in production?

I followed some steps using filters, editing the file web.xml and putting some jars into tomcat library like described in this link , but without success.

Any opinion will be appreciated. Tks in advance.

Instead of trying filter, for CORS you can try and use the following method.

What you can do is modify the method to :

 public Response getYourMethod( HttpServletRequest req) throws Exception, IOException{
  //then your code
 }

Now after that add the following:

since the browser looks for www.mysite.com:3000 in the Allow Origin Header, you need to make sure this is getting added in the following line:

response.setHeader("Access-Control-Allow-Origin", "www.mysite.com:3000");

To get the www.mysite.com:3000 you can use :

request.getRemoteAddr() + ":" + request.getRemotePort();

But if the browser looks for localhost then go for the :

request.getRemoteHost().

Avoid adding * because some browsers will not still allow that and can be rejected by preflight requests.

If you want to add the filters, then look over here

Make sure that you add the headers and methods properly as mentioned in the post.

Hope that solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM