2017-05-30 822 views
1

我正在使用本地開發環境(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://路徑被認爲是已經被安全地傳遞了。「 - 因此,它將和狐狸一樣工作 –

回答

3

鉻認爲本地主機通過HTTP的安全。由於您通過http使用hostnme localhost.example,因此不被視爲安全。

注意:Firefox的行爲與Firefox相似55

0

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/

相關問題