当前位置:网站首页>如何设置跨域隔离启用 SharedArrayBuffer
如何设置跨域隔离启用 SharedArrayBuffer
2022-08-06 04:28:00 【JAVAQXQ】
需求
最近在研究 ffmpeg WebAssembly 版本在网页运行的工具,发现使用到了 SharedArrayBuffer,涉及到跨域隔离的问题,需要设置两个 HTTP 消息头启用跨域隔离:
- Cross-Origin-Opener-Policy 设置为 same-origin(保护源站免受攻击)
- Cross-Origin-Embedder-Policy 设置为 require-corp(保护源站免受侵害)
不同的服务有不同的设置方法,这里简要介绍下。
解决
方案一
对于前端开发来说,本地开发阶段,可以起一个 Node.js 服务,用于本地开发实时调试,比如我用 Express.js (Node.js 后端框架)
// Add headers
app.use(function (req, res, next) {
// 设置响应头
res.header("Cross-Origin-Embedder-Policy", "require-corp");
res.header("Cross-Origin-Opener-Policy", "same-origin");
// Pass to next layer of middleware
next();
});完整的文件可以参考: Create a server with Express.js
方案二
部署到服务器上之后,一般会用 nginx 做代理服务器,这时候可以给 nginx 配置加两个响应头
nginx 配置
location / {
# 设置响应头
add_header 'Cross-Origin-Embedder-Policy' 'require-corp';
add_header 'Cross-Origin-Opener-Policy' 'same-origin';
}方案三
如果只是临时开启简单体验下 SharedArrayBuffer 这个功能,可以在启动谷歌浏览器时加参数。
以 Windows 为例,桌面上找到 chrome 图标,复制一个出来,右击 Chrome 图标 – 属性 – 目标,将原来的
"C:\Program Files\Google\Chrome\Application\chrome.exe"
改成
"C:\Program Files\Google\Chrome\Application\chrome.exe" --enable-features=SharedArrayBuffer
这样用新的图标打开谷歌浏览器就是开启了 SharedArrayBuffer,本地测试用
注意
一旦开启了跨域隔离,可能会对您网站上的其它跨域资源产生影响,比如 Adsense 广告,暂时也没有很好的兼容办法,需要自己做一下取舍。
总结
以上就是我在使用 wasm SharedArrayBuffer 的功能的时候总结出的经验,如果有不对的地方欢迎指出。
边栏推荐
- 14. go channel
- Cloudflare CDN(泛播)IP地址段
- The principle and function of $nextTick
- Cloudflare CDN(泛播)支持转发的网络端口
- 【LaTex】 - 对齐符号&的用法,换行符\\的用法,Misplaced &错误怎么解决
- 瑞吉外卖项目实战Day03
- Getui data intelligence technology practice | Teach you to create data quality ECG, intelligently detect abnormal data "heartbeat"
- golang 处理变量模板
- 电赛电源类题型之问
- Fragment的四种跳转方式
猜你喜欢

2018HBCPC部分题解

【语义分割】2019-DANet CVPR

用户认证——Kerberos部署

Qixi Festival WeChat confession wall applet source code / Laravel-based confession wall WeChat applet source code

瑞吉外卖项目实战Day04

WindowsServer下配置Mysql主从同步---Mysql主从复制同步001
![P1058 [NOIP2008 普及组] 立体图](/img/89/69f66290c7d8b3402ece28479a5f3c.jpg)
P1058 [NOIP2008 普及组] 立体图

YGG 与《元素突击》的创造者游戏平台 G4AL 达成合作

如何写好代码

【Image Classification】2021-Twins NeurIPS
随机推荐
Remove data that exceeds the maximum number of specified List
线程安全的随机数
初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
【LaTex】 - How to use the alignment symbol &, the usage of the newline character \\, how to solve the Misplaced & error
Induction of common annotations in Sring
Getui data intelligence technology practice | Teach you to create data quality ECG, intelligently detect abnormal data "heartbeat"
【Image classification】2021-CvT
2023年第六届先进控制,自动化与机器人国际会议(ICACAR 2023)
Base64与16进制HEX之间的转换
2.cuBLAS开发指南中文版--使用cuBLAS API
Simple sample code of google's lightweight DI framework guice
重发布中的路由策略:
2022年最新辽宁建筑安全员模拟题库及答案
Jvm: synchronized锁定谁?
Translation recommendation|In-depth analysis of BookKeeper protocol model and verification
十四. go channel
“模组+天线”全栈解决方案,提速物联网终端高效部署
信息系统项目管理师必背核心考点(六十一)项目组合概念
项目day(3) 前台环境搭建
IDEA 实用快捷键