本文由 钟意 发布于:,更新于:

浅谈跨域-就你小子不让我跨域

何为跨域

全称: “跨来源资源共享” “Cross-origin resource sharing”

跨域范畴:

  1. 不同主域名
  2. 不同二级域名
  3. 不同端口
  4. http和https协议不同
  5. 域名访问和直接访问其解析IP

造成影响:

  1. Cookie、LocalStorage和IndexDB无法获取
  2. DOM无法获得
  3. AJAX请求不能发送

为何制定跨域

W3C的搞事佬制定的标准, 出发点当然是安全问题.
不妨思考一下古老钓鱼网站的行为, 与我的抽象代码.

第一步

通过个人通信方式把人骗到钓鱼网站

第二步

钓鱼网站已经嵌入了目标官方网站

第三步

(营销语气) 注意看, 这个男人叫王小帅, 他在钓鱼网站上输入了账号密码.

第四步

获取嵌入的官网的DOM节点获取账号密码.

抽象钓鱼代码
1
2
3
4
5
6
7
8
9
...
<iframe name="diaoyu" src="www.xxbank.com"></iframe>
...
<script>
const iframe = window.frames['diaoyu']
const count = iframe.document.getElementById('count')
const pwd = iframe.document.getElementById('password')
console.log("账号:${count}, 密码:${pwd}")
</script>

解决方案

CORS需要浏览器和服务器同时支持。所有浏览器都支持该功能,IE浏览器不能低于IE10。
浏览器一旦发现AJAX请求跨源,就会自动添加一些附加的头信息,有时还会多出一次附加的请求。
因此,实现CORS通信的关键是服务器。只要服务器实现了CORS接口,就可以跨源通信。

了解请求与响应

  1. 简单请求:
    1. 请求method是get、head或者post
    2. 除了用户代理自动设置的一些头部,开发工程师手动设置的头部是如下头部之一: Accept, Accept-Language, Content-Language,
      Content-Type, Last-Event-ID, DPR, Save-Data, Viewport-Width, Width
    3. content-type是application/x-www-form-urlencoded、 multipart/form-data或者text/plain
    4. 没有事件注册到XMLHttpRequestUpload上
    5. 在请求时没有使用ReadableStream

简单请求主要是解决Access-Control-Allow-Origin是否包含在通行域

简单响应
1
2
3
4
//指定允许其他域名访问
'Access-Control-Allow-Origin:http://172.20.0.206'//一般用法(*,指定域,动态设置),3是因为*不允许携带认证头和cookies
//是否允许后续请求携带认证信息(cookies),该值只能是true,否则不返回
'Access-Control-Allow-Credentials:true'
  1. 复杂请求:没错,不满足上面的,都是我啦!
    浏览器会先发送option(预检)请求,
    option请求多了2个字段 Access-Control-Request-Method, Access-Control-Request-Headers
复杂响应
1
2
3
4
5
6
7
8
9
10
//指定允许其他域名访问
'Access-Control-Allow-Origin:http://172.20.0.206'//一般用法(*,指定域,动态设置),3是因为*不允许携带认证头和cookies
//是否允许后续请求携带认证信息(cookies),该值只能是true,否则不返回
'Access-Control-Allow-Credentials:true'
//预检结果缓存时间,也就是上面说到的缓存啦
'Access-Control-Max-Age: 1800'
//允许的请求类型
'Access-Control-Allow-Methods:GET,POST,PUT,POST'
//允许的请求头字段
'Access-Control-Allow-Headers:x-requested-with,content-type'

前端

祖传JSONP

同源策略是根据脚本(js)的来源判断是否限制, jsonp是通过 <script> 标签冒充同源.
缺点是只能发送get请求(聊胜于无?)

  • 原生JS
    原生JS跨域问题示例代码
原生JS
1
2
3
4
5
6
7
8
9
10
11
var script = document.createElement('script');
script.type = 'text/javascript';

// 传参一个回调函数名给后端,方便后端返回时执行这个在前端定义的回调函数
script.src = 'http://www.domain2.com:8080/login?user=admin&callback=handleCallback';
document.head.appendChild(script);

// 回调执行函数
function handleCallback(res) {
alert(JSON.stringify(res));
}
  • JQ
    JQ跨域问题示例代码
Ajax
1
2
3
4
5
6
7
$.ajax({
url: 'http://www.domain2.com:8080/login',
type: 'get',
dataType: 'jsonp', // 请求方式为jsonp
jsonpCallback: "handleCallback", // 自定义回调函数名
data: {}
});
  • Vue
    Vue Axios跨域问题示例代码
Axios
1
2
3
4
5
6
7
this.$http = axios;
this.$http.jsonp('http://www.domain2.com:8080/login', {
params: {},
jsonp: 'handleCallback'
}).then((res) => {
console.log(res);
})
  • Node.js
    NodeJS跨域问题示例代码
Node
1
2
3
4
5
6
7
8
9
10
11
12
13
var querystring = require('querystring');
var http = require('http');
var server = http.createServer();
server.on('request', function(req, res) {
var params = querystring.parse(req.url.split('?')[1]);
var fn = params.callback;
// jsonp返回设置
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.write(fn + '(' + JSON.stringify(params) + ')');
res.end();
});
server.listen('8080');
console.log('Server is running at port 8080...');

前端配置

  • 原生Ajax
    原生Ajax跨域问题示例代码
Ajax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var xhr = new XMLHttpRequest(); // IE8/9需用window.XDomainRequest兼容

// 前端设置是否带cookie
xhr.withCredentials = true;

xhr.open('post', 'http://www.domain2.com:8080/login', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send('user=admin');

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
};
  • jQ Ajax
    JQ Ajax跨域问题配置示例代码
JQAjax
1
2
3
4
5
6
7
8
$.ajax({
...
xhrFields: {
withCredentials: true // 前端设置是否带cookie
},
crossDomain: true, // 会让请求头中包含跨域的额外信息,但不会含cookie
...
});
  • Vue配置
    Vue跨域跨域问题axios,vue-resource配置示例代码
    • axios设置:
      axios.defaults.withCredentials = true
    • vue-resource设置:
      Vue.http.options.credentials = true

后端

java

  • 方案一: WebCrosConfig
    java跨域问题addCorsMappings方案示例代码
配置类
1
2
3
4
5
6
7
8
9
10
11
12
13
@Configuration
public class WebCrosConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
}
  • 方案二: CrosFilter
    java跨域问题CrosFilter方案示例代码
    友人南山客补充方案
CrosFilter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @author 南山客 友情赞助代码
* @email nansker@163.com
* @create 2022/10/10 17:31
* @description
*/

package cn.nansk.takeout.config;

import cn.nansk.takeout.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始进行静态资源映射...");
}

// FIXME: 2022/11/2 跨域问题没有得到解决
//解决跨域问题
//@Override
//public void addCorsMappings(CorsRegistry registry){
// registry.addMapping("/**")
// .allowedOriginPatterns("*")
// .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
// .allowCredentials(true)
// .maxAge(3600)
// .allowedHeaders("*");
//}

/***
* @author Nansker
* @date 2023/2/17 23:17
* @return org.springframework.web.filter.CorsFilter
* @description 允许跨域调用过滤器
* 这里不能使用Override addCorsMappings()方法解决跨域问题,具体原因未知
*/
@Bean
public CorsFilter corsFilter(){
CorsConfiguration config = new CorsConfiguration();
//允许白名单域名进行跨域调用
config.addAllowedOrigin("*");
//允许跨越发送cookie
config.setAllowCredentials(true);
//放行全部原始头信息
config.addAllowedHeader("*");
//允许所有请求方法跨域调用
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}

Node

配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var http = require('http');
var server = http.createServer();
var qs = require('querystring');

server.on('request', function(req, res) {
var postData = '';
// 数据块接收中
req.addListener('data', function(chunk) {
postData += chunk;
});
// 数据接收完毕
req.addListener('end', function() {
postData = qs.parse(postData);

// 跨域后台设置
res.writeHead(200, {
'Access-Control-Allow-Credentials': 'true', // 后端允许发送Cookie
'Access-Control-Allow-Origin': 'http://www.domain1.com', // 允许访问的域(协议+域名+端口)
/*
* 此处设置的cookie还是domain2的而非domain1,因为后端也不能跨域写cookie(nginx反向代{过}{滤}理可以实现),
* 但只要domain2中写入一次cookie认证,后面的跨域接口都能从domain2中获取cookie,从而实现所有的接口都能跨域访问
*/
'Set-Cookie': 'l=a123456;Path=/;Domain=www.domain2.com;HttpOnly' // HttpOnly的作用是让js无法读取cookie
});

res.write(JSON.stringify(postData));
res.end();
});
});
server.listen('8080');
console.log('Server is running at port 8080...');

服务器

Nginx

通过Nginx配置一个代理服务器域名与domain1相同,端口不同)做跳板机,反向代理访问domain2接口,并且可以顺便修改cookie中domain信息,方便当前域cookie写入,实现跨域访问。

站点配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#proxy服务器
server {
listen 81;
server_name www.domain1.com;

location / {
proxy_pass http://www.domain2.com:8080; #反向代理
proxy_cookie_domain www.domain2.com www.domain1.com; #修改cookie里域名
index index.html index.htm;

# 当用webpack-dev-server等中间件代理接口访问nignx时,此时无浏览器参与,故没有同源限制,下面的跨域配置可不启用
add_header Access-Control-Allow-Origin http://www.domain1.com; #当前端只跨域不带cookie时,可为*
add_header Access-Control-Allow-Credentials true;
}
}

Node

node中间件实现跨域代理,原理大致与nginx相同,都是通过启一个代理服务器,实现数据的转发,也可以通过设置cookieDomainRewrite参数修改响应头中cookie中域名,实现当前域的cookie写入,方便接口登录认证。

fetch跨域

get请求

FG前端代码

前端get
1
2
3
4
5
6
7
8
9
10
11
fetch('http://localhost:6888/test_get',{
method: 'GET',
mode: 'cors',
}).then(res => {
return res.json();
}).then(json => {
console.log('获取的结果', json.data);
return json;
}).catch(err => {
console.log('请求错误', err);
})

服务端

服务端配置
1
2
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST")

post请求

FP前端代码

前端post
1
2
3
4
5
6
7
8
9
10
11
12
fetch('http://localhost:6888/test_post',{
method: 'POST',
body: JSON.stringify({name: 'zaozuo'}),
mode: 'cors',
}).then(res => {
return res.json();
}).then(json => {
console.log('获取的结果', json.data);
return json;
}).catch(err => {
console.log('请求错误', err);
})

后端代码同get相同

put请求

把post请求模式改成put即可, 其它一致.
不同于get、post请求的地方是请求有个预检查(OPTIONS请求),然后再发put请求;上面的头部信息都是options请求相关的,put请求跟平时普通http请求一样。

头部补充

  • request跨域头部介绍
    • Access-Control-Allow-Origin:可以允许哪些客户端来访问,指可以是*,也可以是某个域名或者用逗号隔开的域名列表。
    • Access-Control-Expose-Headers: 浏览器可以访问的一些头部。
    • Access-Control-Max-Age:预检查结果可以缓存的问题
    • Access-Control-Allow-Methods:指定客户端发请求可以使用的方法
    • Access-Control-Allow-Headers:指定客户端发请求可以使用的头部。
    • Access-Control-Allow-Credentials: 指定客户端是否可以携带cookie等认证信息(
      前端fetch设置withCredentials:true进行发送cookie),如果是简单请求等跨域得确保此response头设置为true。
  • response头部
    • Access-Control-Allow-Origin:可以允许哪些客户端来访问,指可以是*,也可以是某个域名或者用逗号隔开的域名列表。
    • Access-Control-Expose-Headers: 浏览器可以访问的一些头部。
    • Access-Control-Max-Age:预检查结果可以缓存的问题
    • Access-Control-Allow-Methods:指定客户端发请求可以使用的方法
    • Access-Control-Allow-Headers:指定客户端发请求可以使用的头部。
    • Access-Control-Allow-Credentials: 指定客户端是否可以携带cookie等认证信息(
      前端fetch设置withCredentials:true进行发送cookie),如果是简单请求等跨域得确保此response头设置为true。

奇技淫巧方案

同主域不同子域

实现原理:两个页面都通过js强制设置document.domain为基础主域,就实现了同域。

  • 父窗口
1
2
3
4
5
<iframe id="iframe" src="http://child.domain.com/b.html"></iframe>
<script>
document.domain = 'domain.com';
var user = 'admin';
</script>
  • 子窗口
1
2
3
4
5
<script>
document.domain = 'domain.com';
// 获取父窗口中变量
alert('get js data from parent ---> ' + window.parent.user);
</script>

不同主域

方案一

实现原理:a欲与b跨域相互通信,通过中间页c来实现。三个页面,不同域之间利用iframe的location.hash传值,相同域之间直接js访问来通信。
具体实现:A域:a.html -> B域:b.html -> A域:c.html,a与b不同域只能通过hash值单向通信,b与c也不同域也只能单向通信,但c与a同域,所以c可通过parent.parent访问a页面所有对象。

a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<iframe id="iframe" src="http://www.domain2.com/b.html" style="display:none;"></iframe>
<script>
var iframe = document.getElementById('iframe');

// 向b.html传hash值
setTimeout(function() {
iframe.src = iframe.src + '#user=admin';
}, 1000);

// 开放给同域c.html的回调方法
function onCallback(res) {
alert('data from c.html ---> ' + res);
}
</script>
b.html
1
2
3
4
5
6
7
8
9
<iframe id="iframe" src="http://www.domain1.com/c.html" style="display:none;"></iframe>
<script>
var iframe = document.getElementById('iframe');

// 监听a.html传来的hash值,再传给c.html
window.onhashchange = function () {
iframe.src = iframe.src + location.hash;
};
</script>
c.html
1
2
3
4
5
6
7
<script>
// 监听b.html传来的hash值
window.onhashchange = function () {
// 再通过操作同域a.html的js回调,将结果传回
window.parent.parent.onCallback('hello: ' + location.hash.replace('#user=', ''));
};
</script>

方案二

window.name属性的独特之处:name值在不同页面(甚至不同域名)加载后依旧存在,并且可以支持非常长的name值(2MB).
代理b.html的数据通过iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。

  • doamin1/a.html
  • doamin1/proxy.html
  • domain2/b.html
a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var proxy = function(url, callback) {
var state = 0;
var iframe = document.createElement('iframe');

// 加载跨域页面
iframe.src = url;

// onload事件会触发2次,第1次加载跨域页,并留存数据于window.name
iframe.onload = function() {
if (state === 1) {
// 第2次onload(同域proxy页)成功后,读取同域window.name中数据
callback(iframe.contentWindow.name);
destoryFrame();

} else if (state === 0) {
// 第1次onload(跨域页)成功后,切换到同域代{过}{滤}理页面
iframe.contentWindow.location = 'http://www.domain1.com/proxy.html';
state = 1;
}
};

document.body.appendChild(iframe);

// 获取数据以后销毁这个iframe,释放内存;这也保证了安全(不被其他域frame js访问)
function destoryFrame() {
iframe.contentWindow.document.write('');
iframe.contentWindow.close();
document.body.removeChild(iframe);
}
};

// 请求跨域b页面数据
proxy('http://www.domain2.com/b.html', function(data){
alert(data);
});
proxy.html
1
中间代理页,与a.html同域,内容为空即可。
b.html
1
2
3
<script>
window.name = 'This is domain2 data!';
</script>

postMessage

postMessage是HTML5 XMLHttpRequest Level 2中的API,且是为数不多可以跨域操作的window属性之一,它可用于解决以下方面的问题:

  1. 页面和其打开的新窗口的数据传递
  2. 多窗口之间消息传递
  3. 页面与嵌套的 iframe 消息传递
  4. 上面三个场景的跨域数据传递

用法:postMessage(data,origin)方法接受两个参数:

  • data:html5规范支持任意基本类型或可复制的对象,但部分浏览器只支持字符串,所以传参时最好用JSON.stringify()序列化。
  • origin:协议+主机+端口号,也可以设置为”*“,表示可以传递给任意窗口,如果要指定和当前窗口同源的话设置为”/“。
a.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<iframe id="iframe" src="http://www.domain2.com/b.html" style="display:none;"></iframe>
<script>
var iframe = document.getElementById('iframe');
iframe.onload = function() {
var data = {
name: 'aym'
};
// 向domain2传送跨域数据
iframe.contentWindow.postMessage(JSON.stringify(data), 'http://www.domain2.com');
};

// 接受domain2返回数据
window.addEventListener('message', function(e) {
alert('data from domain2 ---> ' + e.data);
}, false);
</script>
b.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
// 接收domain1的数据
window.addEventListener('message', function(e) {
alert('data from domain1 ---> ' + e.data);

var data = JSON.parse(e.data);
if (data) {
data.number = 16;

// 处理后再发回domain1
window.parent.postMessage(JSON.stringify(data), 'http://www.domain1.com');
}
}, false);
</script>

WebSocket

WebSocket protocol是HTML5一种新的协议。它实现了浏览器与服务器全双工通信,同时允许跨域通讯,是server push技术的一种很好的实现。
原生WebSocket API使用起来不太方便,我们使用Socket.io,它很好地封装了webSocket接口,提供了更简单、灵活的接口,也对不支持webSocket的浏览器提供了向下兼容。

前端代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div>user input:<input type="text"></div>
<script src="./socket.io.js"></script>
<script>
var socket = io('http://www.domain2.com:8080');

// 连接成功处理
socket.on('connect', function() {
// 监听服务端消息
socket.on('message', function(msg) {
console.log('data from server: ---> ' + msg);
});

// 监听服务端关闭
socket.on('disconnect', function() {
console.log('Server socket has closed.');
});
});

document.getElementsByTagName('input')[0].onblur = function() {
socket.send(this.value);
};
</script>
Node-socket后台
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var http = require('http');
var socket = require('socket.io');

// 启http服务
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-type': 'text/html'
});
res.end();
});

server.listen('8080');
console.log('Server is running at port 8080...');

// 监听socket连接
socket.listen(server).on('connection', function(client) {
// 接收信息
client.on('message', function(msg) {
client.send('hello:' + msg);
console.log('data from client: ---> ' + msg);
});

// 断开处理
client.on('disconnect', function() {
console.log('Client socket has closed.');
});
});

本站由 钟意 使用 Stellar 1.25.0 主题创建。
又拍云 提供CDN加速/云存储服务
vercel 提供托管服务
湘ICP备2023019799号-1
总访问 次 | 本页访问