javascript - 如何在网站上安全地使用 google maps api key

标签 javascript api google-maps-api-3 api-key

我在我的网站上使用 google maps api 并想使用我的 API key ,但我对如何保证它的安全感到困惑。我知道我可以将实际 key 硬编码到 index.html 中

<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=HARDCODED_KEY"></script>

但是当任何人查看该站点的源代码时,它就在那里供任何人查看。如果有人使用调试工具(如 Firebug)查看网站,我也不希望它显示在 DOM 中。

我假设有一种方法可以将它存储在我可以获取的单独文件中(可能在我的 html/目录之外)。如果有人可以提供示例或评论这种方法是否相对安全,我将非常感谢您的帮助。我找到了关于这个主题的其他帖子,但没有一个适用于这个(相对简单的)案例。

最佳答案

只需设置引荐来源网址,如 documentation 中所述:

To prevent other applications from using your key and consuming your quota, you can limit the IP addresses that can use your API key to send requests:

Visit the Google Developers Console and log in with your Google account.

Select the project that was created for you when you signed up. The project name will start with Google Maps API for Work.

In the sidebar on the left, select Credentials.

Find the key you're using under the Public API Access heading, and click Edit allowed IPs.

Enter the IP addresses from which your key is to be accepted, one per line. You may also enter a subnet using CIDR notation (e.g. 192.168.0.0/22).

你也可能想出 this question设置推荐后,我想你会发现它很有用。

关于javascript - 如何在网站上安全地使用 google maps api key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32763859/

相关文章:

javascript - 从 MySQL 数据库加载热图 LatLng 坐标 Google map API

javascript - 运行 Javascript 雪 onclick

javascript - 谷歌地图 API : Map width gets resized onclick but route is still loaded with original width

javascript - 兑现 promise ?

java - 从 Django 调用 Java API

javascript - 使用 Laravel 5.4 API 和 Vue.js 进行用户身份验证

javascript - 使用谷歌图表对 JSON API 进行 jQuery 嵌套查询

c# - 为什么我会收到此时区错误,是否有更好的方法在 Google map 时区和 Windows 时区之间进行映射?

javascript - 动态表 onchange 和 td id

javascript - 添加新模型后渲染