跨域配置验证方法
第三方对接羚羊云录像或图片时,需要在羚羊云中央新增对第三方域名的跨域访问配置,否则第三方调用羚羊云的资源时,会提示跨域相关的报错。
此文档提供了允许第三方跨域访问的配置方法和验证跨域配置有效性的方法。
允许第三方跨域访问的配置方法
- 修改羚羊云中央的nginx配置,允许第三方跨域访问
- vim /etc/nginx/map.conf (添加需要访问的域名)
map http_origin corsHost {default 'https://antelopecloud.cn';~^(.*).topvdn.com(:(\d+))?$ $http_origin;~^(.*).antelopecloud.cn(:(\d+))?$ $http_origin;~^(.*).lingda.com(:(\d+))?$ $http_origin;~^(.*).lingdanet.com(:(\d+))?$ $http_origin;~^(.*).netposa.com(:(\d+))?$ $http_origin;}
- vim /opt/nginx/html/crossdomain.xml (添加需要访问的域名)
<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><allow-access-from domain="*.topvdn.com" /><allow-access-from domain="*.antelopecloud.cn" /><allow-access-from domain="*.lingdanet.com" /><allow-access-from domain="*.netposa.com" /><allow-http-request-headers-from domain="*.topvdn.com" headers="*" /><allow-http-request-headers-from domain="*.antelopecloud.cn" headers="*" /><allow-http-request-headers-from domain="*.lingdanet.com" headers="*" /><allow-http-request-headers-from domain="*.netposa.com" headers="*" /></cross-domain-policy>
- 修改完后需 reload nginx
验证跨域配置有效性的方法
- curl 验证方法
curl -i -X GET \'https://xxxx' \ # 羚眸地址或中央地址-H 'Origin: www.nccxgh.com' \-H 'Content-Type: application/json' \-H 'cache-control: no-cache'
- html 验证方法
找台机器启一个nginx服务,并记录其IP地址
在本机
hosts文件配置第三方域名到nginx服务IP地址的静态解析获取一个播放录像的URL
方法之一是打开羚眸页面,播放录像回放,按F12键可在浏览器开发者工具中获取录像的播放地址。部署测试页面到nginx服务
在下列测试页面代码中找到
跨域测试请修改一下播放地址注释项,将source修改为上一步骤获取的录像播放地址。保存后(如:test.html),将测试文件放到nginx服务默认站点的根目录。
测试
在本机,打开浏览器,地址栏输入http://第三方域名/test.html,能播放录像,则配置成功。
<!DOCTYPE html><html><head><meta charset="utf-8" /><meta http-equiv="x-ua-compatible" content="IE=edge" /><metaname="viewport"content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"/><title>Aliplayer在线配置</title><linkrel="stylesheet"href="https://g.alicdn.com/de/prismplayer/2.8.2/skins/default/aliplayer-min.css"/><scripttype="text/javascript"charset="utf-8"src="https://g.alicdn.com/de/prismplayer/2.8.2/aliplayer-min.js"></script></head><body><div class="prism-player" id="player-con"></div><button id="btn">停止</button><script>var player = new Aliplayer({id: "player-con",width: "100%",height: "500px",autoplay: true,isLive: true,playsinline: true,preload: true,controlBarVisibility: "hover",useH5Prism: true,// 跨域测试请修改一下播放地址source: "https://jxsr-api.antelopecloud.cn/cloud/v2/live/538462729.m3u8?client_token=538462729_0_1590540869_7de2d3012b8abbdf7a6ddac92388583b",},function (player) {console.log("播放器创建了。");});document.getElementById("btn").onclick = function () {console.log(11);player.pause();};</script></body></html>
