标签:
同源策略限制一个加载于A origin的document或者script能够如何和来自于另外一个origin的resource交互。同源策略是隔离潜在恶意网页的安全机制。
两个网页只有具有相同的protocol,port以及host才被认为是具有相同的origin的。
比如http://xxx.yyy.com:8000/zzz/page.html和http://xxx.yyy.com:8000/kkk/index.html具有相同的origin
about:blank
, javascript:
and data:
URLs则从加载那个URL的文件中继承origin.
同源策略控制了异源之间的互操作,比如,当你使用XMLHttpRequest或者一个<img>元素时就存在这个问题。这些互操作(interactions)典型地放在三个category中:
# Apache config
<FilesMatch ".(eot|ttf|otf|woff)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
X-Frame-Options
头来防止你自己的网页被别人frame过去!
var invocation = new XMLHttpRequest(); var url = ‘http://bar.other/resources/public-data/‘; function callOtherDomain() { if(invocation) { invocation.open(‘GET‘, url, true); invocation.onreadystatechange = handler; invocation.send(); } }
GET /resources/public-data/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://foo.example/examples/access-control/simpleXSInvocation.html
Origin: http://foo.example
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Server: Apache/2.0.61
Access-Control-Allow-Origin: *
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/xml
如何阻止跨域(源)访问呢?
Cross-orgin script API access
Javascript API,比如iframe.contentWindow, window.parent, window.open,window.opener允许documents来直接引用彼此。当两个document不同源时,这些reference则仅对Window和Location对象开放相当有限的访问权限,下面将分别列出。
https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
标签:
原文地址:http://www.cnblogs.com/kidsitcn/p/5036682.html