10 methods for cross-origin Resource Sharing
Same-origin policy
In client programming languages such as JavaScript and ActionScript, the same-source policy is a very important security concept, which has an important significance in ensuring data security. The same-origin policy specifies that scripts in different domains are isolated. scripts in one domain cannot access and operate most of the attributes and methods in another domain. So what is the same domain and what is different domain? When two domains have the same protocol (such as HTTP), the same port (such as 80), and the same host (such as www.example.org), we can think that they are the same domain. For example, https://www.example.org, http://www.example.org: 8080, any two in the http://sub.example.org will constitute a cross-domain. The same-origin policy should also handle some special situations, such as limiting the access permissions of scripts under the file protocol. Local HTML files are opened through the file protocol in the browser. If the script can access any other files on the hard disk through the file protocol, security risks may occur, at present, IE8 still has such risks.
Cross-origin Resource Sharing is restricted by the same-origin policy. However, with the development of practices and browsers, we have gained a lot of valuable experience in cross-origin request skills. Here I divide cross-origin Resource Sharing into two types: one is one-way data requests and the other is two-way message communication. Next, I will list some common cross-origin methods. The source code of the following cross-origin instances can be obtained from here.
Unidirectional cross-domain jsonp
Jsonp (JSON with padding) is a simple and efficient cross-origin method. The script tag in HTML can load and execute Javascript in other domains, therefore, we can dynamically load resources in other domains using SCRIPT tags. For example, if I want to load the data of Domain B from the pagea page of Domain A, then in the pageb page of Domain B, I declare the data required by pagea in the form of JavaScript, then, in pagea, use the script tag to load pageb, and the script in pageb will be executed. Jsonp adds a callback function. After pageb loads the function, it executes the function defined in pagea. The required data is passed to the function in the form of parameters. Jsonp is easy to implement, but it also has some security risks. If a third-party script is executed at will, it can tamper with the page content and intercept sensitive data. However, jsonp is a perfect choice for both trusted Parties to transmit data.
Flash urlloader
Flash has its own security policy. The server can use crossdomain. an XML file is used to declare which domains can be accessed by SWF files. SWF can also use APIs to determine which domains can be loaded by SWF files. When cross-origin resource access, for example, requests data from the domain www.a.com to the domain www. B .com, we can use flash to send HTTP requests. First, modify crossdomain. xml on the domain www. B .com (usually stored in the root directory, if not manually created) and add www.a.com TO THE whitelist. Second, send HTTP requests through Flash urlloader, and finally pass the response results to JavaScript through Flash API. Flash urlloader is a common cross-origin solution. However, if you need to support iOS, this solution is powerless.
Access Control
Access control is a cross-origin method that exclusive to each other. Currently, it is only supported in a few browsers. These browsers can send a cross-origin HTTP Request (Firefox, Google Chrome, etc. through XMLHttpRequest, in IE8), the request response must contain an HTTP Response Header of access-control-allow-origin, which declares the access permission of the Request domain. For example, if www.a.com sends a cross-origin HTTP request to asset. php under www. B .com, the following response header must be added to asset. php:
header("Access-Control-Allow-Origin: http://www.a.com"); |
Window. Name
The name attribute of the window object is a very special attribute. When the location of the window changes and is reloaded, its name attribute can remain unchanged. In this case, we can use IFRAME to load page B of other domains in page A, while in page B, we use JavaScript to assign the data to window. after name and IFRAME are loaded, page a modifies the IFRAME address and changes it to an address in the same domain. Then the window can be read. the value of name. This method is suitable for one-way data requests, and the protocol is simple and secure. External scripts are not executed without restrictions like jsonp.
Server proxy
When the data provider does not support the jsonp protocol or the window. Name protocol or has no access permission to other domains, we can capture data through the server proxy. For example, when a user requests the resource file asset.txt under www.a.com, an ajaxrequest directed to www. B .com/asset.txtwill be blocked by the browser. At this time, we configure a proxy under www.a.com, and then bind the Ajax request to this proxy path, such as www.a.com/proxy/. Then the proxy sends an HTTP request to asset.txt under www. B .com. cross-origin HTTP requests are made on the server, the client does not generate cross-origin Ajax requests. This cross-origin method does not need to sign agreements with the target resource and is aggressive. In addition, it should be noted that the proxy should be protected to a certain extent in practice, such as limiting the use or frequency of others.
Bidirectional cross-Origin document. Domain
By modifying the domain attribute of the document, we can communicate between the domain and the subdomain or different subdomains. The same domain policy assumes that the domain and subdomain belong to different domains. For example, www.a.com and sub.a.com are different domains. At this time, we cannot call the JavaScript method defined in sub.a.com on the page under www.a.com. But when we change the domain attribute of their document to a.com, the browser will think that they are in the same domain, then we can call each other's method to communicate with each other.
FIM-fragment identitier messaging
Javascript can only perform limited access and operations between different domains. In fact, we can use these limited access permissions to achieve cross-origin communication. FIM (fragment identitier messaging) was invented on this premise. The parent window can perform URL reading and writing on IFRAME, and IFRAME can also read and write the URL of the parent window. A part of the URL is called frag, which is the # and its subsequent characters. It is generally used for browser anchor location, the server does not care about this part. It should be said that the HTTP request process will not carry frag, so this part of modification will not generate HTTP requests, but will generate browser history records. The fim principle is to change the URL's frag for bidirectional communication. Each window sends messages by changing the location of other windows, and listens for changes in its own URL to receive messages. This communication method may cause unnecessary browser history records. In addition, some browsers do not support the onhashchange event. You need to poll the changes to the URL. Finally, the URL has a length limit in the browser, this restricts the amount of data transferred each time.
Flash localconnection
Two-way communication on the page can also be solved through flash. The flash API has the localconnection class, which allows two SWF to communicate through the process, at this time, SwF can be played in an independent Flash Player or air, or embedded in an HTML page or PDF. Following this communication principle, we can nest a SwF on the HTML pages of different domains to transfer data to each other. SWF exchange data quickly through localconnection, but each time the data volume has a size limit of 40 kb. Cross-origin communication using this method is too complex and requires two SWF files, which is not practical.
Window. postmessage
Window. postmessage is a new method defined in html5. this method can easily communicate across windows. Because it is a new method, it cannot be used in both old and old browsers.
Cross Frame
Cross frame is a variant of FIM. It uses a blank IFRAME to generate no excessive browser history or change the URL, the availability and performance have been greatly improved. When sending a message in response, a hidden IFRAME will be created on the page. The srcof iframedirects to proxyb.html and uses the message as the URL response to obtain the iframe url, parse the message, and remove the IFRAME. When B .html#sends a message to a.html, the principle is the same. Cross frame is a good two-way communication method and is safe and efficient, but it cannot be used in opera. However, in opera, we can use a simpler window. postmessage instead.
Summary
There are many cross-origin methods. We can find the most suitable solution for different application scenarios. For example, for one-way data requests, we should prioritize jsonp or window. name, two-way communication we adopt cross frame. We can also use server proxy to capture data without reaching a communication protocol with the data provider.
Source: http://www.ued163.com /? P = 1800