SPRINGMVC support cross-domain requests in front-end separation development mode

Source: Internet
Author: User

1. Add Cors rule Support in Web. XML (please modify the package name)

<filter>    <filter-name>cors</filter-name>    <filter-class>com ... common.filter.simplecorsfilter</filter-class></filter><filter-mapping>    < filter-name>cors</filter-name>    <url-pattern>/*</url-pattern></ Filter-mapping>

2, Spring.xml add corsmapping(please modify the package name)

class= "Com...common.filter.webconfig"/>

3, create corsmapping and Corsfilter class

Webconfig.java as follows:(please change the package name)
 PackageCom...common.filter;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.servlet.config.annotation.CorsRegistry;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter, @Configuration @enablewebmvc Public classWebconfigextendsWebmvcconfigureradapter {@Override Public voidaddcorsmappings (Corsregistry registry) {registry.addmapping ("/**"). Allowedorigins ("*"). Allowedmethods ("PUT", "DELETE", "POST", "GET", "Options"). Allowedheaders ("X-requested-with", "Authorization"). Allowcredentials (false). MaxAge (3600); }}

< Span style= "COLOR: #cc7832" > < Span style= "COLOR: #6a8759" > < Span style= "COLOR: #cc7832" > < Span style= "COLOR: #cc7832" > 

Simplecorsfilter. Java as follows:(please modify the package name)
 PackageCom...common.filter;Importjavax.servlet.*;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException; Public classSimplecorsfilterImplementsFilter {@Override Public voidInit (Filterconfig filterconfig)throwsservletexception {} @Override Public voidDoFilter (servletrequest req, servletresponse Res, filterchain chain)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) req; HttpServletResponse Response=(HttpServletResponse) res; Response.setheader ("Access-control-allow-origin", Request.getheader ("Origin")); Response.setheader ("Access-control-allow-methods", "POST, GET, OPTIONS, DELETE"); Response.setheader ("Access-control-allow-credentials", "true"); Response.setheader ("Access-control-max-age", "3600"); Response.setheader ("Access-control-allow-headers", "X-requested-with,access-control,content-type");    Chain.dofilter (req, res); } @Override Public voiddestroy () {}}

SPRINGMVC support cross-domain requests in front-end separation development mode

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.