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