为什么我在本地主机上的Chrome中出现getCurrentPosition()和watchPosition()“不安全起源”错误?

问题描述:

我正在使用本地开发环境(Ubuntu 16.04)在网站上工作,并通过http://localhost.example/在Chrome上测试网站(58) - 它连接到本地Web服务器。为什么我在本地主机上的Chrome中出现getCurrentPosition()和watchPosition()“不安全起源”错误?

运行此Javascript:

$(document).ready(function() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } 
}); 

触发此错误:

[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details.

这是为什么?我了解,面向公众的网站需要运行HTTPS才能使地理位置库/功能正常工作。我们有许多公共网站通过HTTPS运行类似的代码。

但是根据depreciation documentation

localhost is treated as a secure origin over HTTP, so if you're able to run your server from localhost, you should be able to test the feature on that server.

以上的JavaScript在线经由http://localhost.example/test-page/加载HTML体运行 - 所以为什么我收到“不安全的起源”的错误在Chrome?

Firefox(53)按预期方式显示浏览器访问位置提示。

+0

您可以使用https运行它吗? – ControlAltDel

+1

也许是因为'localhost'!==''localhost.example' –

+0

也是,Firefox从版本55开始也会有https限制 - 在firefox中''本地交付的文件如http:// localhost和file://路径被认为是已经被安全地传递了。“ - 因此,它将和狐狸一样工作 –

铬认为本地主机通过HTTP的安全。由于您通过http使用hostnme localhost.example,因此不被视为安全。

注意:Firefox的行为与Firefox相似55

SSL over HTTP协议确保客户端和服务器内的私人通信。信息可能在传输时通过专用网络传输。网络上的任何第三人(黑客)都可以窃取该信息。为了避免该浏览器强制用户使用安全连接。

在本地服务器上,信息不会超出我们的专用本地网络,因为不需要这种安全性。所以我们可以期待浏览器在本地服务器上允许没有SSL的地理定位。理想情况下,浏览器开发人员应该跳过localhost和127.0.0.1或类似来源的验证。

必须有可用来避免这样的问题,即你可以在本地服务器上安装的自签名证书的技巧,或者你可以编辑浏览器配置文件允许的域来访问地理位置,摄像头等等

有用链接,

https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins

https://ngrok.com/