With regard to cross-domain problems, the main use of the more is Cros cross-domain. For more information please see Https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS However, when Cross-domain requests are supported under SPRINGMVC+ANGULARJS, A situation in which a complex cross-domain scenario (post + JSON) failed. The starting Cross-domain configuration is as follows:
public class Crossinterceptor extends Handlerinterceptoradapter {
@Override public
boolean Prehandle ( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Response.AddHeader
(" Access-control-allow-origin "," * ");
Response.AddHeader ("Access-control-allow-methods", "*");
Response.AddHeader ("Access-control-max-age", "the");
Response.AddHeader ("Access-control-allow-headers", "Content-type");
Response.AddHeader ("Access-control-allow-credentials", "false");
return Super.prehandle (Request, response, handler);
}
The Spring-dispatcher-servlet.xml is configured as follows:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path= "/**/*"/>
<bean class= " Cn.***.filter. Crossinterceptor "/>
</mvc:interceptor>
</mvc:interceptors>
No problem for simple cross-domain. However, the Post+json request failed, prompting for a cross-domain failure. Tracking the Dooption method in Springmvc source to Frameworkservlet, found that the option Prefetch was accepted, but spring actively returned to allow and did not support Cross-domain configuration. Therefore, add the new configuration as follows:
public class Crossfilter extends Onceperrequestfilter {
@Override
protected void dofilterinternal ( HttpServletRequest request, HttpServletResponse response, Filterchain Filterchain) throws Servletexception, IOException {
if (Request.getheader ("Access-control-request-method")!= null && "Options". Equals ( Request.getmethod ()) {
//CORS "pre-flight" Request
Response.AddHeader ("Access-control-allow-origin", "*") ;
Response.AddHeader ("Access-control-allow-methods", "Get, POST, put, DELETE");
Response.AddHeader ("Access-control-allow-headers", "Content-type");
Response.AddHeader ("Access-control-max-age", "1800");//30 min
}
filterchain.dofilter (request, response);
}
The Web.xml configuration is as follows:
<filter>
<filter-name>cors</filter-name>
<filter-class>cn.***.filter. crossfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors </filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
At this point, option requests that the Crossfilter filter be accessed and given a cross-domain response header, as well as a dooption method in the Frameworkservlet. Looking at the browser console, the option request returns support for Cross-domain information and subsequent post requests enter Controller.