当前位置:网站首页>Understand │ what is cross domain? How to solve cross domain problems?
Understand │ what is cross domain? How to solve cross domain problems?
2022-07-27 18:07:00 【InfoQ】
What is cross-domain
- Domain :It means that the browser cannot execute scripts of other websites
- Cross domain :It's run by the browserThe same-origin policyCaused by the , It 's the browser
JavaScriptSecurity restrictions imposed , The so-called homology ( I mean in the same domain ) Two pages have the same protocolprotocol, hosthostAnd port numberportWill causeCross domain

Cross domain scenario
- What are the cross domain scenarios of the scenario , Please refer to the table below

There are four ways to solve cross domain problems
- nginx Reverse proxy of
- Use
nginxReverse proxy implementation across domains , It's the easiest way to cross domains
- It just needs to be modified
nginxIs configured to solve cross-domain problems , Support for all browsers , Supportsession, You don't need to change any code , It does not affect server performance
// nginx To configure
server {
listen 81;
server_name www.domain1.com;
location / {
proxy_pass http://www.domain2.com:8080; # Reverse proxy
proxy_cookie_domain www.domain2.com www.domain1.com; # modify cookie In the domain name
index index.html index.htm;
# When used webpack-dev-server Such as middleware proxy interface access nignx when , No browser is involved at this time , So there is no homologous restriction , The following cross-domain configuration is not enabled
add_header Access-Control-Allow-Origin http://www.domain1.com; # The current end is cross-domain only cookie when , for *
add_header Access-Control-Allow-Credentials true;
}
}- jsonp request
jsonpIt is a common method of cross source communication between server and client . The biggest characteristic is simple and applicable , Compatibility is good.Compatible with lower versions IE, The disadvantage is that it only supportsgetrequest , I won't support itpostrequest
- The principle of web pages is to add a
<script>Elements , Request... From the serverjsondata , After the server receives the request , Put the data in the parameter position of a callback function with a specified name and pass it back
//jquery Realization
<script>
$.getJSON('http://autofelix.com/api.php&callback=?', function(res) {
// Process the data obtained
console.log(res)
});
</script>- Back end language agent
- You can transfer it through a language without cross domain restrictions , Request resources through the back-end language , And then it returns the data
- such as
http://www.autofelix.cnNeed to callhttp://api.autofelix.cn/userinfoTo get user data , Because the subdomain name is different , There will be cross domain restrictions
- You can ask first
http://www.autofelix.cnUnder thephpfile , such ashttp://www.autofelix.cn/api.php, And then through thatphpThe file returns data
<?php
// api.php Code in file
public function getCurl($url, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = getCurl('http://api.autofelix.cn/userinfo');
return $result;- Back end language settings
- It is mainly through the back-end language to actively set cross domain requests , Here we use
phpAs a case study
<?php
// Allow access to all domain names
header('Access-Control-Allow-Origin: *');
// Allow a single domain name to access
header('Access-Control-Allow-Origin: https://autofelix.com');
// Allow multiple custom domain names to access
static public $originarr = [
'https://autofelix.com',
'https://baidu.com',
'https://csdn.net',
];
// Get the current cross domain domain name
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
if (in_array($origin, self::$originarr)) {
// allow $originarr In the array Domain name cross domain access
header('Access-Control-Allow-Origin:' . $origin);
// Response type
header('Access-Control-Allow-Methods:POST,GET');
// belt cookie Cross domain access to
header('Access-Control-Allow-Credentials: true');
// Response header Settings
header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
}边栏推荐
- Notes on standardized management of "ancestral warehouse" of meituan meal
- 工信部再治数据安全,网易易盾“隐私合规”守住企业经营底线
- Wechat applet realizes location map display and introduces map map without navigation
- Does PostgreSQL 14 support winserver2022?
- Some suggestions for writing original technical articles
- What should we pay attention to when choosing the LED display screen of the stadium
- JDBC连接数据库读取前台无法显示数据
- The Ministry of industry and information technology re governs data security, and Netease Yidun "privacy compliance" keeps the bottom line of enterprise operation
- 【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)
- Taishan Office Technology Lecture: word strange paragraph borders
猜你喜欢

js实现右键菜单栏功能

What should we pay attention to when choosing the LED display screen of the stadium

Zhengzhou University database course resource description

JSP custom tag (bottom)

快解析结合海典医药

泰山OFFICE技术讲座:WORD奇怪的段落边框

Behind every piece of information you collect, you can't live without TA
![[single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer](/img/8d/27d99bf7a94e59f158ba952e9239bd.png)
[single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer

知物由学 | 从0到1搭建实时反外挂机制,多维度补充手游攻防力

An end-to-end file upload program based on form, including client and server
随机推荐
Some suggestions for writing original technical articles
[MCU] 2.2 pin function of AT89S52
Common shell commands (1) -- variable case conversion
Mysql: function
Evaluation index of machine learning (II) -- classification evaluation index
【数据库系统概论(王珊)】第11章 并发控制
Zhengzhou University database course resource description
Configuration and basic use of vim
Yyds dry inventory interview must brush top101: specified interval reversal in the linked list
MySQL view and stored procedure
数据库超话(四)
Does PostgreSQL 14 support winserver2022?
[introduction to database system (Wang Shan)] Chapter 5 - database integrity
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?
Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples
Database hyperphone (II)
美团到餐“祖传数仓”标准化治理笔记
灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
Summer Challenge [FFH] real time chat room websocket practice
Database hyperphone (III)