
定义
跨域是指从一个域名的网页去请求另一个域名的资源
跨域背景
限制原因
如果一个网页可以随意地访问另外一个网站的资源,那么就有可能在客户完全不知情的情况下出现安全问题
为什么要跨域
公司内部有多个不同的子域,比如一个是location.company.com ,而应用是放在app.company.com , 这时想从 app.company.com去访问 location.company.com 的资源就属于跨域
java解决方式
在servlet中对response进行设置:
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
spring boot注解
部分服务接口设置跨域访问
接受所有请求
添加注解@CrossOrigin
接受特定请求、形式
@CrossOrigin(origins = https://domain2.com,maxAge = 3600,methods = {RequestMethod.GET, RequestMethod.POST})
全局设置
