Spring mvc 跨域配置

两种方式:

  1. Controller中使用注解
  2. xml中使用全局配置

这里记录xml配置:

<mvc:cors>
		<mvc:mapping path="/api/**"
		allow-credentials="true"
		allowed-headers="x-requested-with,content-type"
		allowed-methods="GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE"
		max-age="360"
		allowed-origins="http://video.test.com,http://www.test.com"/>
</mvc:cors>

为了保证ajax跨域访问sessionID一致,请求中增加

 xhrFields:{withCredentials: true},
 crossDomain: true,

如:

$.ajax({
    url:url,
    xhrFields:{withCredentials: true},
    crossDomain: true,

	success:function(result){
	   alert("OK");
	},
	error:function(){}
});