Update Cors

master
yutent 2023-11-02 17:03:57 +08:00
parent fa49c230ba
commit 1c63b1e67e
1 changed files with 15 additions and 11 deletions

26
Cors.md

@ -1,17 +1,22 @@
框架内置有一个 "一键配置跨域支持" 的中间件。非常简单实用。
## 跨域中间件
框架内置**跨域中间件**, 支持一键启用, 便可轻松实现跨域访问。
`注意: 这里的跨域配置, 只针对常规浏览器的跨域处理。并不能阻止其他方式的请求。`
```js
{
cors: {
enabled: false,
credentials: false,
origin: [], // ['abc.com', 'a.foo.com']
credentials: false, // 如果要支持cookie,需要设为 true
origin: [], // 默认不限制, 需要时可配置多个域名 ['abc.com', 'a.foo.com']
maxAge: 14400
}
}
```
从[默认配置](https://github.com/bytedo/gmf.core/wiki/默认配置)中可以看出, 我们想要开启跨域支持, 最简单的做法, 只需要把里面的enabled的值设为true即可。
从[默认配置](/gm5/core/wiki/默认配置)中可以看出, 我们想要开启跨域支持, 最简单的做法, 只需要把里面的enabled的值设为true即可。
即, 在入口文件中, 加入以下代码即可开启:
@ -24,7 +29,12 @@ app.set({ cors: { enabled: true } })
注意: 这里的域名配置中, 根域名和二级域名、三级域名等, 可认为是同一域名, 只需要填写根域名即可。
```js
app.set({ cors: { enabled: true, origin: ['aaa.com', 'bbb.com'] } }) // 这里不需要再写 'foo.aaa.com' 等二级域名、三级域名了。
app.set({
cors: {
enabled: true,
origin: ['aaa.com', 'bbb.com']
}
}) // 这里不需要再写 'foo.aaa.com' 等二级域名、三级域名了。
```
@ -38,9 +48,3 @@ app.set({ cors: { enabled: true, credentials: true } })
----
上一节: [⟪路由⟫](https://github.com/bytedo/gmf.core/wiki/Router)
下一节: [⟪控制器⟫](https://github.com/bytedo/gmf.core/wiki/Controller)