All Projects → FatDong1 → Cross Origin

FatDong1 / Cross Origin

🌀 跨域demo。CORS、JSONP、postMessage、websocket、document.domain、window.name、iframe等示例

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Cross Origin

Instagram Proxy Api
CORS compliant API to access Instagram's public data
Stars: ✭ 245 (-48.42%)
Mutual labels:  jsonp, proxy, cors
Workers
Cloudflare Workers
Stars: ✭ 111 (-76.63%)
Mutual labels:  proxy, cors
Cloudflare Cors Anywhere
CORS "anywhere" proxy in a Cloudflare worker. DEMO at: https://test.cors.workers.dev/
Stars: ✭ 162 (-65.89%)
Mutual labels:  proxy, cors
Learn Nginx
Nginx 入门指南
Stars: ✭ 728 (+53.26%)
Mutual labels:  proxy, cors
Php Whois
PHP WHOIS provides parsed and raw whois lookup of domains and ASN routes. PHP 5.4+ and 7+ compatible
Stars: ✭ 179 (-62.32%)
Mutual labels:  proxy, domain
Bus
Bus 是一个基础框架、服务套件,它基于Java8编写,参考、借鉴了大量已有框架、组件的设计,可以作为后端服务的开发基础中间件。代码简洁,架构清晰,非常适合学习使用。
Stars: ✭ 253 (-46.74%)
Mutual labels:  proxy, cors
Java Sec Code
Java web common vulnerabilities and security code which is base on springboot and spring security
Stars: ✭ 1,033 (+117.47%)
Mutual labels:  jsonp, cors
Ssl Proxy
🔒 Simple zero-config SSL reverse proxy with real autogenerated certificates (LetsEncrypt, self-signed, provided)
Stars: ✭ 427 (-10.11%)
Mutual labels:  proxy
Disqusjs
💬 Render Disqus comments in Mainland China using Disqus API
Stars: ✭ 455 (-4.21%)
Mutual labels:  proxy
Chromium
Chromium browser with SSL VPN. Use this browser to unblock websites.
Stars: ✭ 4,041 (+750.74%)
Mutual labels:  proxy
Httpteleport
Transfer 10Gbps http traffic over 1Gbps networks :)
Stars: ✭ 422 (-11.16%)
Mutual labels:  proxy
Shadowsocks Deepin
😎 a powful shadowsocks client for deepin
Stars: ✭ 429 (-9.68%)
Mutual labels:  proxy
Proxymanager
🎩✨🌈 OOP Proxy wrappers/utilities - generates and manages proxies of your objects
Stars: ✭ 4,556 (+859.16%)
Mutual labels:  proxy
Awesome Web Scraping
List of libraries, tools and APIs for web scraping and data processing.
Stars: ✭ 4,510 (+849.47%)
Mutual labels:  proxy
Fetch Some Proxies
Simple Python script for fetching "some" (usable) proxies
Stars: ✭ 470 (-1.05%)
Mutual labels:  proxy
Rawgithack
Serves files from github, bitbucket and gitlab, but with the correct content types
Stars: ✭ 422 (-11.16%)
Mutual labels:  proxy
Xx Mini
👻 XX-Net 精简版
Stars: ✭ 472 (-0.63%)
Mutual labels:  proxy
Smocker
Smocker is a simple and efficient HTTP mock server and proxy.
Stars: ✭ 465 (-2.11%)
Mutual labels:  proxy
Fanqiang
翻墙-科学上网
Stars: ✭ 23,428 (+4832.21%)
Mutual labels:  proxy
Information collection handbook
Handbook of information collection for penetration testing and src
Stars: ✭ 447 (-5.89%)
Mutual labels:  domain

跨域demo集

一个关于跨域的简单demo集合,适合入门学习跨域的。

任何对初学者学习跨域有用的PR都会接受,谢谢!

原理

用node的web框架express在3000端口3001端口分别开启一个静态服务器,在两个端口间进行跨域传输数据。

  • 3000端口: 为前端界面,前端静态资源放在3000端口的服务器上
  • 3001端口: 为后端接口服务器

目录

  1. Demo0: 直接跨域,会报什么错?
  2. Demo1: CORS跨域
  3. Demo2: JSONP跨域
  4. Demo3: postMessage跨域
  5. Demo4: window.name跨域
  6. Demo5: location.hash跨域
  7. Demo6: document.domain跨域
  8. Demo7: 后端设置代理proxy跨域
  9. Demo8: WebSocket跨域
  10. Demo9: form表单跨域
  11. Demo10: 关闭浏览器跨域限制

Set Up

运行环境

  • node.js 全局安装
  • npm 全局安装

运行步骤

  1. git clone [email protected]:FatDong1/cross-domain
  2. cross-domain目录下npm install,下载项目所需的依赖
  3. 进入对应的demo目录,①node serverRes.js ②重新开一个终端窗口 ③node serverReq.js
  4. 或者在对应的demo目录下,终端窗口输入bash run.bat或者使用脚本run.sh,直接完成上一步的①②③

Demo0: 直接跨域,会报什么错?

项目运行起来之后

点击按钮请求3001端口数据,会报错,如下图所示。


error.png

Demo1: CORS跨域

项目运行起来之后

常考面试题

  • CORS的请求过程(简单请求、复杂请求)。MDN

Demo2: JSONP跨域

项目运行起来后

常考面试题

  • JSONP的流程
  • JSONP的实现原理

Demo3: postMessage跨域

这是html5的新API,适用于不同窗口iframe之间的跨域

项目运行地址: http://localhost:3000

Demo4: window.name跨域

http://localhost:3000/a.html 使用js动态生成一个隐藏的iframe,设置src属性为' http://localhost:3001/c.html ',等这个iframe加载完之后,重新设置src属性为同源的地址' http://localhost:3000/b.html '(b.html是一个空的html文件),现在iframe与a.html同源,那就可以访问window.name属性,而name值没有变化,因为window.name属性在不同的页面(甚至不同域名)加载后依旧存在。

项目运行地址: http://localhost:3000/a.html

Demo5: location.hash跨域

http://localhost:3000/a.html 使用js动态生成一个隐藏的iframe,设置src属性为' http://localhost:3001/c.html#getdata ',在c.html判断hash值是否为'#getdata',如果为'#getdata',则在当前的iframe(c.html)中再生成一个隐藏的iframe,其src属性指向' http://localhost:3000/b.html ',因为a.html和b.html同源,所以可以在b.html里面修改a.html的hash值,这样a.html就可以通过获取自身的hash值得到数据

项目运行地址: http://localhost:3000/a.html

Demo6: document.domain跨域

document.domain设置成自身或更高一级的父域,且主域必须相同。 【服务器已过期,域名无法使用,demo作废】

注意!!

这是挂在我自己腾讯云域名(xuhaodong.cn)上的demo,所以抱歉这个demo无法在本地运行。不过我只是把demo放到了云服务器上,代码还是一样的,没有修改过。

线上访问地址: http://a.xuhaodong.cn/a.html

如果要在本地运行的话,需要搭建一个本地web服务器, 参考链接:https://github.com/web2hack/proxy

Demo7: 后端设置代理proxy跨域

因为JS同源策略是浏览器的安全策略,所以在浏览器客户端不能跨域访问,而服务器端调用HTTP接口只是使用HTTP协议,不会执行JS脚本,不需要同源策略,也就没有跨越问题。简单地说,就是浏览器不能跨域,后台服务器可以跨域。

  • demo1 通过使用http-proxy-middleware插件设置后端的代理,在 http://localhost:3000 运行

  • demo2 不使用插件配置代理,直接使用http模块发出请求, 在 http://localhost:3000 运行

Demo8: WebSocket跨域

WebSocket是一种通信协议,使用ws://(非加密)和wss://(加密)作为协议前缀。该协议不实行同源政策,只要服务器支持,就可以通过它进行跨源通信。

项目运行地址: http://localhost:3000/a.html

Demo9: form表单跨域

Demo10: 关闭浏览器跨域限制

可以使用chrome插件CORS

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].