Vue项目中的Referrer Policy相关问题

2019-11-10  本文已影响0人  sxh不是帅小伙

如果大家在开发Vue项目时遇到了有关Referrer Policy的问题,类似下面的情况:


错误图片

那么这种问题就是Referrer Policy问题。首先我们先了解一下什么是Referrer Policy

Referrer简介

简单来说,Referrer 是HTTP协议中的一个请求报头,用于告知服务器用户的来源页面。比如说你从Google搜索结果中点击进入了某个页面,那么此次HTTP请求中的Referrer 就是Google搜索结果页面的地址。如果你的某篇博客中引用了其他地方的一张图片,那么对该图片的HTTP请求中的Referrer 就是你那篇博客的地址。
一般Referrer 主要用于统计,像CNZZ、百度统计等可以通过Referrer 统计访问流量的来源和搜索的关键词(包含在URL中)等等,方便站长们有针性对的进行推广和SEO什么的
当然Referrer 另一个用处就是防盗链了,主要是图片和网盘服务器使用的较多。盗链的危害不言而喻,侵犯了版权不说,增加了服务器的负荷,却没有给真正的服务提供者带来实际利益(广告点击什么的)。
另外注意的是,Referrer是浏览器为我们自动加上的。

Referrer Policy简介

Referrer Policy的作用就是的作用就是为了控制请求头中referrer的内容,目前是一个候选标准,不过已经有部分浏览器支持该标准。
目前Referrer-Policy只包含以下几种值:

示例

Policy Document Navigation to Referrer
no-referrer https://example.com/page.html any domain or path no referrer
no-referrer-when-downgrade https://example.com/page.html https://example.com/otherpage.html https://example.com/page.html
no-referrer-when-downgrade https://example.com/page.html https://mozilla.org https://example.com/page.html
no-referrer-when-downgrade https://example.com/page.html http://example.org no referrer
origin https://example.com/page.html any domain or path https://example.com/
origin-when-cross-origin https://example.com/page.html https://example.com/otherpage.html https://example.com/page.html
origin-when-cross-origin https://example.com/page.html https://mozilla.org https://example.com/
origin-when-cross-origin https://example.com/page.html http://example.com/page.html https://example.com/
same-origin https://example.com/page.html https://example.com/otherpage.html https://example.com/page.html
same-origin https://example.com/page.html https://mozilla.org no referrer
strict-origin https://example.com/page.html https://mozilla.org https://example.com/
strict-origin https://example.com/page.html http://example.org no referrer
strict-origin http://example.com/page.html any domain or path http://example.com/
strict-origin-when-cross-origin https://example.com/page.html https://example.com/otherpage.html https://example.com/page.html
strict-origin-when-cross-origin https://example.com/page.html https://mozilla.org https://example.com/
strict-origin-when-cross-origin https://example.com/page.html http://example.org no referrer
unsafe-url https://example.com/page.html any domain or path https://example.com/page.html

Vue中全局修改Referrer的方法

在项目的router.js文件中加入以下代码:

let head = document.getElementsByTagName('head');
let meta = document.createElement('meta');
meta.name = 'referrer';
//根据实际情况修改referrer的值,可选值参考上文
meta.content = 'no-referrer';
head[0].appendChild(meta);

改变referrer的值重新运行项目可以看到请求成功!


正确图片
上一篇 下一篇

猜你喜欢

热点阅读