Tomcat adds the cross-domain access feature-access-control-allow-origin:*

Source: Internet
Author: User
Tags tomcat server

Target: The Tomcat server provides an interface that cannot be accessed in other domains and requires an additional access-control-allow-origin:*

There are many methods of direct configuration, but I have not succeeded, so I can write the interceptor myself.

1. Writing Interceptor Code

Create a new class, implement the filter interface, the code is as follows

Package Com.itxc.filter;import Javax.servlet.*;import Javax.servlet.http.httpservletresponse;import Java.io.ioexception;public class Corsfilter implements Filter {    @Override public    void Init (filterconfig Filterconfig) throws servletexception {    }    @Override public    void DoFilter (ServletRequest servletrequest, Servletresponse Servletresponse, Filterchain filterchain) throws IOException, servletexception {        HttpServletResponse HttpResponse = (httpservletresponse) servletresponse;        Httpresponse.addheader ("Access-control-allow-origin", "*");        Filterchain.dofilter (ServletRequest, servletresponse);    }    @Override public    Void Destroy () {    }}

2. Configure Web. xml

    <filter>        <filter-name>CorsFilter</filter-name>        <filter-class> com.itxc.filter.corsfilter</filter-class>    </filter>    <filter-mapping>        < filter-name>corsfilter</filter-name>        <url-pattern>/*</url-pattern>    </ Filter-mapping>

3. Remove the mess configuration you've previously configured for this feature.

It's done.

Tomcat adds the cross-domain access feature-access-control-allow-origin:*

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.