Tomcat add Cros cross-origin request

Source: Internet
Author: User

Remote access to rest via tools will appear

Cross-origin requests have been blocked: the same-origin policy prohibits reading remote resources located in Http://XXXXX:1200/gyly/busData. (Reason: CORS request failed). Modify method Add Filter
Import Java.io.ioexception;import Javax.servlet.filterchain;import Javax.servlet.servletexception;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.filter.onceperrequestfilter;public class Corsfilter extends Onceperrequestfilter {@Override    protected void dofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain)            throws Servletexception, IOException {    Response.AddHeader ("Access-control-allow-origin", "*") ;    Response.AddHeader ("Access-control-allow-methods",            "GET, POST, PUT, DELETE, OPTIONS");    Response.AddHeader ("Access-control-allow-headers",            "origin, Content-type, accept, X-requested-with, Sid, Mycustom, Smuser ");        Filterchain.dofilter (request, response);}    }

Modify Web. xml
<filter>  <filter-name>CorsFilter</filter-name>  <filter-class> Com.interfaceservice.filter.corsfilter</filter-class></filter><filter-mapping>  < Filter-name>corsfilter</filter-name>  <url-pattern>/*</url-pattern></filter-mapping >



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tomcat add Cros cross-origin request

Related Article

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.