forked from shfshanyue/Daily-Question
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db2d416
commit 7d4553f
Showing
42 changed files
with
835 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 什么情况下会发送 OPTIONS 请求 | ||
|
||
|
||
|
||
::: tip Issue | ||
欢迎在 Issue 中交流与讨论: [Issue 363](https://github.com/shfshanyue/Daily-Question/issues/363) | ||
::: | ||
|
||
::: tip Author | ||
回答者: [hedongxiaoshimei](https://github.com/hedongxiaoshimei) | ||
::: | ||
|
||
[搬运地址](https://blog.csdn.net/kahhy/article/details/81563063) | ||
1:请求的方法不是GET/HEAD/POST | ||
2:POST请求的Content-Type 异常 | ||
3:请求设置了自定义的header字段 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# CORS 如果需要指定多个域名怎么办 | ||
|
||
|
||
|
||
::: tip Issue | ||
欢迎在 Issue 中交流与讨论: [Issue 364](https://github.com/shfshanyue/Daily-Question/issues/364) | ||
::: | ||
|
||
::: tip Author | ||
回答者: [shfshanyue](https://github.com/shfshanyue) | ||
::: | ||
|
||
`CORS` 通过控制 `Access-Control-Allow-Origin` 控制哪些域名可以共享资源,取值如下 | ||
|
||
``` bash | ||
Access-Control-Allow-Origin: <origin> | * | ||
``` | ||
|
||
其中 `*` 代表所有域名,`origin` 代表指定特定域名,那如何设置多个域名了? | ||
|
||
此时需要通过代码实现,**根据请求头中的 `Origin` 来设置响应头 `Access-Control-Allow-Origin`**,那 Origin 又是什么东西? | ||
|
||
## 请求头: Origin | ||
|
||
并不是所有请求都会自动带上 `Origin`,在浏览器中带 `Origin` 的逻辑如下 | ||
|
||
1. 如果存在跨域,则带上 `Origin`,值为当前域名 | ||
1. 如果不存在跨域,则不带 `Origin` | ||
|
||
逻辑理清楚后,关于服务器中对于 `Access-Control-Allow-Origin` 设置多域名的逻辑也很清晰了 | ||
|
||
1. 如果请求头不带有 `Origin`,证明未跨域,则不作任何处理 | ||
1. 如果请求头带有 `Origin`,证明跨域,根据 `Origin` 设置相应的 `Access-Control-Allow-Origin: <Origin>` | ||
|
||
使用伪代码实现如下: | ||
|
||
``` js | ||
// 获取 Origin 请求头 | ||
const requestOrigin = ctx.get('Origin'); | ||
|
||
// 如果没有,则跳过 | ||
if (!requestOrigin) { | ||
return await next(); | ||
} | ||
|
||
// 设置响应头 | ||
ctx.set('Access-Control-Allow-Origin', requestOrigin) | ||
``` | ||
|
||
## Vary: Origin | ||
|
||
此时可以给多个域名控制 CORS,但此时假设有两个域名访问 `static.shanyue.tech` 的跨域资源 | ||
|
||
1. `foo.shanyue.tech`,响应头中返回 `Access-Control-Allow-Origin: foo.shanyue.tech` | ||
1. `bar.shanyue.tech`,响应头中返回 `Access-Control-Allow-Origin: bar.shanyue.tech` | ||
|
||
看起来一切正常,但如果中间有缓存怎么办? | ||
|
||
1. `foo.shanyue.tech`,响应头中返回 `Access-Control-Allow-Origin: foo.shanyue.tech`,被 CDN 缓存 | ||
1. **`bar.shanyue.tech`,因由缓存,响应头中返回 `Access-Control-Allow-Origin: foo.shanyue.tech`,跨域出现问题** | ||
|
||
此时,`Vary: Origin` 就上场了,代表为不同的 `Origin` 缓存不同的资源 | ||
|
||
## 总结 (简要答案) | ||
|
||
CORS 如何指定多个域名? | ||
|
||
**根据请求头中的 `Origin` 来设置响应头 `Access-Control-Allow-Origin`**,思路如下 | ||
|
||
1. 总是设置 `Vary: Origin`,避免 CDN 缓存破坏 CORS 配置 | ||
1. 如果请求头不带有 `Origin`,证明未跨域,则不作任何处理 | ||
1. 如果请求头带有 `Origin`,证明浏览器访问跨域,根据 `Origin` 设置相应的 `Access-Control-Allow-Origin: <Origin>` | ||
|
||
使用伪代码实现如下 | ||
|
||
``` js | ||
// 获取 Origin 请求头 | ||
const requestOrigin = ctx.get('Origin'); | ||
|
||
ctx.set('Vary', 'Origin') | ||
|
||
// 如果没有,则跳过 | ||
if (!requestOrigin) { | ||
return await next(); | ||
} | ||
|
||
// 设置响应头 | ||
ctx.set('Access-Control-Allow-Origin', requestOrigin) | ||
``` | ||
|
||
> 相关问题:[如何避免 CDN 为 PC 端缓存移动端页面](https://github.com/shfshanyue/Daily-Question/issues/330) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 既然 cors 配置可以做跨域控制,那可以防止 CSRF 攻击吗 | ||
|
||
|
||
|
||
::: tip Issue | ||
欢迎在 Issue 中交流与讨论: [Issue 366](https://github.com/shfshanyue/Daily-Question/issues/366) | ||
::: | ||
|
||
::: tip Author | ||
回答者: [shfshanyue](https://github.com/shfshanyue) | ||
::: | ||
|
||
**对 CORS 一点用也没有** | ||
|
||
1. **`form` 提交不通过 `CORS` 检测**,你可以在本地进行测试 | ||
1. 即使通过 `xhr` 及 `fetch` 进行提交被 CORS 拦住,**但是对于简单请求而言,请求仍被发送**,已造成了攻击 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.