前端常见跨域解决方案
2018-03-23 13:07
483 查看
本文基于https://www.cnblogs.com/roam/p/7520433.html整理
同源是指协议+域名+端口都相同,是浏览器最和县的安全功能,如果没有同源策略,浏览器容易受到XSS,CSFR等攻击。
同源策略限制以下几种行为:1)cookie 、loaclstorage和indexDB无法读取;2)DOM和JSd对象无法huode,3)AJAX请求不能发送
常见跨域场景
URL 说明 是否允许通信
http://www.domain.com/a.js
http://www.domain.com/b.js 同一域名,不同文件或路径 允许
http://www.domain.com/lab/c.js
http://www.domain.com:8000/a.js
http://www.domain.com/b.js 同一域名,不同端口 不允许
http://www.domain.com/a.js
https://www.domain.com/b.js 同一域名,不同协议 不允许
http://www.domain.com/a.js
http://192.168.4.12/b.js 域名和域名对应相同ip 不允许
http://www.domain.com/a.js
http://x.domain.com/b.js 主域相同,子域不同 不允许
http://domain.com/c.js
http://www.domain1.com/a.js
http://www.domain2.com/b.js 不同域名 不允许
跨域解决方案
1)通过jsonp跨域
2)document.domain+iframe跨域
3)location.hash+ifream
4)window.name+ifream
5)postMessage跨域
6)跨域资源共享CORS
7)nginx代理跨域
8)nodejs中间件代理跨域
9)webSocket协议跨域
10)图像Ping
原生实现
jquery+ajax的实现
优点:实现了浏览器和服务器的双向通信,可以直接访问响应问呢
缺点:从其他域加载资源可能存在安全问题,只能实现get请求
实现原理:两个页面都通过js强行设置document.domain为基础主域,就实现了同域
1)父窗口(www.domain.com/a.html)
2.)子窗口:(child.domain.com/b.html)
具体实现:A域:a.html -> B域:b.html -> A域:c.html,a与b不同域只能通过hash值单向通信,b与c也不同域也只能单向通信,但c与a同域,所以c可通过parent.parent访问a页面所有对象。
1.)a.html:(www.domain1.com/a.html)
2.)b.html:(www.domain2.com/b.html)
1.)a.html:(www.domain1.com/a.html)
2.)proxy.html:(www.domain1.com/proxy….
中间代理页,与a.html同域,内容为空即可。
3.)b.html:(www.domain2.com/b.html)
>
总结:通过iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。
页面和其他的新窗口的数据传递
多窗口之间的消息传递
页面与嵌套的ifream之间的消息传递
上面三个场景的跨域数据传递
用法:PostMessage(data,origin)
data:传递时最好用json.stringify()序列化
origin:协议+主机+端口 也可以设置为“*”,表示可以传递给任意窗口,如果要指定和当前窗口同源的话用“/”
1.)a.html:(www.domain1.com/a.html)
2.)b.html:(www.domain2.com/b.html)
带cookie的请求:前后端都需要设置字段,另外注意所带cookie为跨域请求接口所在域的cookie,并非当前页
目前所有浏览器都支持该功能,IE8/9需要使用XDomianRequest对象支持CORS。
1、 前端设置:
1.)原生ajax
2.)jQuery ajax
2、 服务端设置:
若后端设置成功,前端浏览器控制台则不会出现跨域报错信息,反之,说明没设成功。
1.)Java后台:
浏览器跨域 访问JS CSS IMG等常规静态文件被同源策略允许,但iconfont字体文件(eot|otf|ttf|woff|svg)例外,此时可在nginx的静态资源服务器中加入以下配置。
nginx方向代理接口跨域
跨域原理:同源策略是安全策略不是HTTP协议的一部分。服务端调用HTTP接口只是使用HTTP协议不会执行JS脚本,不需要同源策略,也就不存在跨域问题。
实现思路:通过nginx配置一个代理服务器(域名与domain1相同,端口不同)做跳板机,反向代理访问domain2接口,并且可以顺便修改cookie中domain信息,方便当前域cookie写入,实现跨域登录。
proxy服务器
1.) 前端代码示例:
1、 非vue框架的跨域(2次跨域)
利用node + express + http-proxy-middleware搭建一个proxy服务器。
1.)前端代码示例:
2.)中间件服务器:
1.)前端代码:
2.)Nodejs socket后台:
图像Ping是与服务器进行简单单向跨域通信的一种方式,请求数据通过查询字符串发送,响应可以是任意内容(通常为像素图或者204响应)。我们利用JS创建一个新的Image对象,并把src属性设置为指向请求的地址,通过监听onload和onerror事件来确定是否接受到了响应。
只能发送get请求,无法访问服务器响应文本,单向通信。
前端跨域是指在浏览器同源策略的限制下去请求另一个域下的资源
同源策略同源是指协议+域名+端口都相同,是浏览器最和县的安全功能,如果没有同源策略,浏览器容易受到XSS,CSFR等攻击。
同源策略限制以下几种行为:1)cookie 、loaclstorage和indexDB无法读取;2)DOM和JSd对象无法huode,3)AJAX请求不能发送
常见跨域场景
URL 说明 是否允许通信
http://www.domain.com/a.js
http://www.domain.com/b.js 同一域名,不同文件或路径 允许
http://www.domain.com/lab/c.js
http://www.domain.com:8000/a.js
http://www.domain.com/b.js 同一域名,不同端口 不允许
http://www.domain.com/a.js
https://www.domain.com/b.js 同一域名,不同协议 不允许
http://www.domain.com/a.js
http://192.168.4.12/b.js 域名和域名对应相同ip 不允许
http://www.domain.com/a.js
http://x.domain.com/b.js 主域相同,子域不同 不允许
http://domain.com/c.js
http://www.domain1.com/a.js
http://www.domain2.com/b.js 不同域名 不允许
跨域解决方案
1)通过jsonp跨域
2)document.domain+iframe跨域
3)location.hash+ifream
4)window.name+ifream
5)postMessage跨域
6)跨域资源共享CORS
7)nginx代理跨域
8)nodejs中间件代理跨域
9)webSocket协议跨域
10)图像Ping
通过jsonp跨域
为了减轻web服务器的负载,把js ,css,img等静态资源分离到另一台独立域名的服务器上,在html页面上在=再通过响应的标签从不同域名下加载静态资源,是被浏览器允许的。基于此原理,可以通过过动态创建<script>标签,再请求一个带参数的网址实现跨域通信
原生实现
<script> var script = document.createElement('script'); script.type = 'text/javascript'; // 传参并指定回调执行函数为onBack script.src = 'http://www.domain2.com:8080/login?user=admin&callback=onBack'; document.head.appendChild(script); // 回调执行函数 function onBack(res) { alert(JSON.stringify(res)); } </script> 服务端返回如下(返回时即执行全局函数): onBack({"status": true, "user": "admin"})
jquery+ajax的实现
$.ajax({ url: 'http://www.domain2.com:8080/login', type: 'get', dataType: 'jsonp', // 请求方式为jsonp jsonpCallback: "onBack", // 自定义回调函数名 data: {} });
优点:实现了浏览器和服务器的双向通信,可以直接访问响应问呢
缺点:从其他域加载资源可能存在安全问题,只能实现get请求
document.domain+ifream跨域
此方案仅限于主域相同,子域不同的情况实现原理:两个页面都通过js强行设置document.domain为基础主域,就实现了同域
1)父窗口(www.domain.com/a.html)
<iframe id="iframe" src="http://child.domain.com/b.html"></iframe> <script> document.domain = 'domain.com'; var user = 'admin'; </script>
2.)子窗口:(child.domain.com/b.html)
<script> document.domain = 'domain.com'; // 获取父窗口中变量 alert('get js data from parent ---> ' + window.parent.user); </script>
location.hash+ifream跨域
实现原理:a要与b通信,通过中间也c来实现。三个页面,不同域之间利用ifream的location.hash传值。相同域之间通过js访问来通信。具体实现:A域:a.html -> B域:b.html -> A域:c.html,a与b不同域只能通过hash值单向通信,b与c也不同域也只能单向通信,但c与a同域,所以c可通过parent.parent访问a页面所有对象。
1.)a.html:(www.domain1.com/a.html)
<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>
2.)b.html:(www.domain2.com/b.html)
<iframe id="iframe" src="http://www.domain1.com 4000 /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> 3.)c.html:(www.domain1.com/c.html)
<script> // 监听b.html传来的hash值 window.onhashchange = function () { // 再通过操作同域a.html的js回调,将结果传回 window.parent.parent.onCallback('hello: ' + location.hash.replace('#user=', '')); }; </script>
window.name+ifream跨域
window.name属性的独特之处在于:name值在不同页面甚至不同域加载后依旧存在,并且可以支持长的name值1.)a.html:(www.domain1.com/a.html)
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); });
2.)proxy.html:(www.domain1.com/proxy….
中间代理页,与a.html同域,内容为空即可。
3.)b.html:(www.domain2.com/b.html)
<script> window.name = 'This is domain2 data!'; </script
>
总结:通过iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。
postMessage跨域
postMmessage是HTML5 HttpRequest Level2中的API,且是为数不多可以跨域操作的window属性之一,可以解决以下方面的问题:页面和其他的新窗口的数据传递
多窗口之间的消息传递
页面与嵌套的ifream之间的消息传递
上面三个场景的跨域数据传递
用法:PostMessage(data,origin)
data:传递时最好用json.stringify()序列化
origin:协议+主机+端口 也可以设置为“*”,表示可以传递给任意窗口,如果要指定和当前窗口同源的话用“/”
1.)a.html:(www.domain1.com/a.html)
<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>
2.)b.html:(www.domain2.com/b.html)
<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>
跨域资源共享CORS
普通跨域请求:只服务端设置Access-Control-allow-Origin即可,前端无需设置带cookie的请求:前后端都需要设置字段,另外注意所带cookie为跨域请求接口所在域的cookie,并非当前页
目前所有浏览器都支持该功能,IE8/9需要使用XDomianRequest对象支持CORS。
1、 前端设置:
1.)原生ajax
// 前端设置是否带cookie xhr.withCredentials = true; 示例代码: 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); } };
2.)jQuery ajax
$.ajax({ ... xhrFields: { withCredentials: true // 前端设置是否带cookie }, crossDomain: true, // 会让请求头中包含跨域的额外信息,但不会含cookie ... });
2、 服务端设置:
若后端设置成功,前端浏览器控制台则不会出现跨域报错信息,反之,说明没设成功。
1.)Java后台:
/* * 导入包:import javax.servlet.http.HttpServletResponse; * 接口参数中定义:HttpServletResponse response */ response.setHeader("Access-Control-Allow-Origin", "http://www.domain1.com"); // 若有端口需写全(协议+域名+端口) response.setHeader("Access-Control-Allow-Credentials", "true");
nginx代理跨域
nginx配置解决iconfont跨域浏览器跨域 访问JS CSS IMG等常规静态文件被同源策略允许,但iconfont字体文件(eot|otf|ttf|woff|svg)例外,此时可在nginx的静态资源服务器中加入以下配置。
location / { add_header Access-Control-Allow-Origin *; }
nginx方向代理接口跨域
跨域原理:同源策略是安全策略不是HTTP协议的一部分。服务端调用HTTP接口只是使用HTTP协议不会执行JS脚本,不需要同源策略,也就不存在跨域问题。
实现思路:通过nginx配置一个代理服务器(域名与domain1相同,端口不同)做跳板机,反向代理访问domain2接口,并且可以顺便修改cookie中domain信息,方便当前域cookie写入,实现跨域登录。
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; } }
1.) 前端代码示例:
var xhr = new XMLHttpRequest(); // 前端开关:浏览器是否读写cookie xhr.withCredentials = true; // 访问nginx中的代理服务器 xhr.open('get', 'http://www.domain1.com:81/?user=admin', true); xhr.send();
Nodejs中间件代理跨域
node中间件实现跨域代理,原理大致与nginx相同,都是通过启一个代理服务器,实现数据的转发。1、 非vue框架的跨域(2次跨域)
利用node + express + http-proxy-middleware搭建一个proxy服务器。
1.)前端代码示例:
var xhr = new XMLHttpRequest(); // 前端开关:浏览器是否读写cookie xhr.withCredentials = true; // 访问http-proxy-middleware代理服务器 xhr.open('get', 'http://www.domain1.com:3000/login?user=admin', true); xhr.send();
2.)中间件服务器:
var express = require('express'); var proxy = require('http-proxy-middleware'); var app = express(); app.use('/', proxy({ // 代理跨域目标接口 target: 'http://www.domain2.com:8080', changeOrigin: true, // 修改响应头信息,实现跨域并允许带cookie onProxyRes: function(proxyRes, req, res) { res.header('Access-Control-Allow-Origin', 'http://www.domain1.com'); res.header('Access-Control-Allow-Credentials', 'true'); }, // 修改响应信息中的cookie域名 cookieDomainRewrite: 'www.domain1.com' // 可以为false,表示不修改 })); app.listen(3000); console.log('Proxy server is listen at port 3000...');
WebSocket协议跨域
WebSocket protocol是HTML5一种新的协议。它实现了浏览器与服务器全双工通信,同时允许跨域通讯,是server push技术的一种很好的实现。原生WebSocket API使用起来不太方便,我们使用Socket.io,它很好地封装了webSocket接口,提供了更简单、灵活的接口,也对不支持webSocket的浏览器提供了向下兼容。1.)前端代码:
<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>
2.)Nodejs socket后台:
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.'); }); });
图像Ping
原理:一个网页可以从任何网页加载图像不用担心跨域。图像Ping是与服务器进行简单单向跨域通信的一种方式,请求数据通过查询字符串发送,响应可以是任意内容(通常为像素图或者204响应)。我们利用JS创建一个新的Image对象,并把src属性设置为指向请求的地址,通过监听onload和onerror事件来确定是否接受到了响应。
var img=new Image(); img.onload=img.onError=function(){ alert("Done"); } img.src="http://www.example.com/test?name='Nicholes'"
只能发送get请求,无法访问服务器响应文本,单向通信。