第一种方式
Controller上添加注解@CrossOrigin
@CrossOrigin(origins = "http://xxxxx:8080",allowedHeaders = "*",
allowCredentials = "true",methods = {RequestMethod.GET,RequestMethod.POST})
@Controller
@RequestMapping("/api")
public class ZlbClientController {
spring-mvc.xml添加如下配置
<!--<import resource="spring-mvc-shiro.xml"/>-->
<!--<import resource="spring-speed-up.xml"/>-->
<!--web socket相关,需要tomcat8 的支持-->
<!-- 配置接口跨域请求-->
<mvc:cors>
<!--
allowed-origins="*" 表示跨域方案对所有请求都生效
allowed-methods="POST,GET,DELETE,PUT" 允许对请求方式
allowed-headers="Content-Type, Access-Control-Allow-Headers,Authorization,X-Requested-with" 允许对请求头
allow-credentials="true" 表示是否可以将对请求的响应暴露给页面
-->
<mvc:mapping path="/**" allowed-origins="*"
allowed-methods="POST,GET,DELETE,PUT"
allowed-headers="Content-Type, Access-Control-Allow-Headers,Authorization,X-Requested-with"
allow-credentials="true"
/>
</mvc:cors>
第二种方式
nginx添加如下配置
location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
}