首页 > 解决方案 > Firebase firestore 应用程序 - 使用网络调用是一种好习惯吗?暴露 apiKey 可以让别人让你有额外的账单吗?

问题描述

我正在开发一个网络应用程序,它依赖于通过 Cordova 的 Firebase firestore 进行实时同步。

我尝试使用一个特定的插件但没有成功。

我最终只使用了这样的网络电话:

索引.html

我加载了

https://www.gstatic.com/firebasejs/5.8.0/firebase-app.js

https://www.gstatic.com/firebasejs/5.8.0/firebase-firestore.js

我加到头上

<meta http-equiv="Content-Security-Policy" content="
    default-src 'self' *.googleapis.com data: gap:
        https://ssl.gstatic.com;
    script-src 'self' 'unsafe-inline' 'unsafe-eval'
        https://*.gstatic.com https://*.googleapis.com;
    style-src 'self' 'unsafe-inline';
    media-src *;
    connect-src *
">

index.js

firebase.initializeApp({
     apiKey: 'my_api_key',
     authDomain: 'localhost',
     projectId: 'my_projectid'
});

var db = firebase.firestore();     

function loadMessages() {
    var query = firebase.firestore()
                    .collection('my_collection');

    // Start listening to the query.
    query.onSnapshot(function(snapshot) {
        snapshot.docChanges().forEach(function(change) {
            if (change.type === 'removed') {
                console.log("deleted"+change.doc.id);
            } else {
                var message = change.doc.data();

                //change color and text
                console.log(change.doc.id+": "+message.status);
                }
            }
        });
    });
}

// We load currently existing chat messages and listen to new ones.
loadMessages();

它有效,但我想知道这种方法是否存在任何安全风险?

特别是因为 api_key 仍然暴露...

编辑

正如在底部评论中提到的:

向公众公开 Firebase apiKey 是否安全?

如何防止我的 apikey 被其他人使用,然后被 Google 收取我未生成的呼叫的费用?有没有办法只允许来自我的应用程序“com.example.app”的呼叫?

谢谢

标签: javascriptfirebasecordovagoogle-cloud-firestore

解决方案


推荐阅读