添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
温文尔雅的野马  ·  curl.setopt·  2 天前    · 
坏坏的山羊  ·  SpringBoot2.X ...·  4 天前    · 
火爆的山楂  ·  Spring ...·  4 天前    · 
严肃的火锅  ·  FileNotFoundError: ...·  4 周前    · 
喝醉的爆米花  ·  Getting an error as ...·  1 月前    · 
暗恋学妹的消防车  ·  issue in error ...·  3 月前    · 

特别注意两点:

第一 :如果是协议和端口造成的跨域问题“前台”是无能为力的;

第二 :在跨域问题上,域仅仅是通过“ URL 的首部”来识别而不会去尝试判断相同的 ip 地址对应着两个域或两个域是否在同一个 ip 上。

同源策略限制以下几种行为

1.) Cookie、LocalStorage 和 IndexedDB 无法读取
2.) DOM 和 Js对象无法获得
3.) AJAX 请求不能发送

二.跨域解决方案

1、 通过jsonp跨域

2、 跨域资源共享(CORS

3、nodejs中间件代理跨域(webpack

4、 nginx反向代理

5、 postMessage跨域

6、WebSocket协议跨域

7、 document.domain + iframe跨域

8、 location.hash + iframe

9、 window.name + iframe跨域

1 .通过jsonp跨域

通常为了减轻web服务器的负载,我们把jscssimg等静态资源分离到另一台独立域名的服务器上,在html页面中再通过相应的标签从不同域名下加载静态资源,而被浏览器允许,也就是说:

  • script
  • iframe
  • 通过这些属性引入的链接地址,没有同源限制;

    基于此原理,我们可以通过动态创建script,再请求一个带参网址实现跨域通信。原理:

  • 服务器段返回数据是通过看起来执行了func的形式将数据和func函数转换成字符串返回的;
  • 客户端收到该字符串后,经过解析就会执行该函数;
  • 所以,Func必须是一个全局函数;
  • 并且JSONP需要服务器支持;
  • 1.)原生实现:

     <script>
        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));
     </script>
    

    服务端返回如下(返回时即执行全局函数):

    handleCallback({"status": true, "user": "admin"})
    

    2.)vue.js:

    this.$http.jsonp('http://www.domain2.com:8080/login', {
        params: {},
        jsonp: 'handleCallback'
    }).then((res) => {
        console.log(res); 
    

    后端node.js代码示例:

    var querystring = require('querystring');
    var http = require('http');
    var server = http.createServer();
    server.on('request', function(req, res) {
        var params = qs.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...');
    
  • 只能处理get一种请求。由于JSONP借助于script标签的 src属性实现的,而src属性都是资源文件请求,也就是get请求。
  • 不安全,被截获串改,返回木马也执行;
  • 2.跨域资源共享(CORS)

    普通跨域请求

    只服务端设置Access-Control-Allow-Origin即可,前端无须设置,若要带cookie请求:前后端都需要设置。比较常用

    需注意的是

    由于同源策略的限制,所读取的cookie为跨域请求接口所在域的cookie,而非当前页。如果想实现当前页cookie的写入,可参考下文:七、nginx反向代理中设置proxy_cookie_domain 和 八、NodeJs中间件代理中cookieDomainRewrite参数的设置。

    目前,所有浏览器都支持该功能(IE8+IE8/9需要使用XDomainRequest对象来支持CORS)),CORS也已经成为主流的跨域解决方案。

    前端设置:

    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.)vue框架

    a.) axios设置:

    axios.defaults.withCredentials = true
    

    b.) vue-resource设置:

    Vue.http.options.credentials = true
    

    服务端设置

    若后端设置成功,前端浏览器控制台则不会出现跨域报错信息,反之,说明没设成功。

    1.)Nodejs后台示例:

    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...');
    

    设置Access-Control-Allow-Origin头信息时,只能设置一个具体地址或通配符;

    并且一旦设置为了通配符就不再允许携带cookie了;

    3.nodejs中间件代理跨域(webpack)

    结合webpack和webpack-dev-server 实现http请求代理;

    只需要在webpack.config.js中配置devServer即可:

    devServer: {
    	port: 3000,
        progress: true,
        contentBase: './build',
        proxy: {
            '/':{
                target: 'http://127.0.0.1:3000',
                changeOrigin: true
    

    4.ngnix反向代理

    不需要前端进行任何操作,仅靠后端完成;

    5.postMessage

    postMessage是HTML5 XMLHttpRequest Level 2中的API,且是为数不多可以跨域操作的window属性之一,它可用于解决以下方面的问题:
    a.) 页面和其打开的新窗口的数据传递
    b.) 多窗口之间消息传递
    c.) 页面与嵌套的iframe消息传递
    d.) 上面三个场景的跨域数据传递

    用法:postMessage(data,origin)方法接受两个参数
    data: html5规范支持任意基本类型或可复制的对象,但部分浏览器只支持字符串,所以传参时最好用JSON.stringify()序列化。
    origin: 协议+主机+端口号,也可以设置为"*",表示可以传递给任意窗口,如果要指定和当前窗口同源的话设置为"/"。

    1.)a.html:(http://www.domain1.com/a.html)

    <iframe id="iframe" src="http://www.domain2.com/b.html" style=";"></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:(http://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>
    

    6.WebSocket协议跨域

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

    1.)前端代码:

    <div>user input:<input type="text"></div>
    <script src="https://cdn.bootcss.com/socket.io/2.2.0/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.'); 
    

    7.document.domain + iframe跨域

    此方案仅限主域相同,子域不同的跨域应用场景。

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

    1.)父窗口:(http://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.)子窗口:(http://child.domain.com/b.html)

    <script>
        document.domain = 'domain.com';
        // 获取父窗口中变量
        alert('get js data from parent ---> ' + window.parent.user);
    </script>
    

    8.location.hash + iframe跨域

    实现原理: 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页面所有对象。

    1.)a.html:(http://www.domain1.com/a.html)

    <iframe id="iframe" src="http://www.domain2.com/b.html" style=";"></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:(http://www.domain2.com/b.html)

    <iframe id="iframe" src="http://www.domain1.com/c.html" style=";"></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:(http://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>
    

    9.window.name + iframe跨域

    window.name属性的独特之处:name值在不同的页面(甚至不同域名)加载后依旧存在,并且可以支持非常长的 name 值(2MB)。

    1.)a.html:(http://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:(http://www.domain1.com/proxy....)
    中间代理页,与a.html同域,内容为空即可。

    3.)b.html:(http://www.domain2.com/b.html)

    <script>
        window.name = 'This is domain2 data!';
    </script>
    

    总结:通过iframe的src属性由外域转向本地域,跨域数据即由iframe的window.name从外域传递到本地域。这个就巧妙地绕过了浏览器的跨域访问限制,但同时它又是安全操作。

    多抽出1分钟来学习,让你的生命更加精彩!