了解面向 Cloud Storage 的 Firebase 安全规则
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
安全性通常是应用开发过程中最复杂的部分之一。在大多数应用中,开发者所构建和运行的服务器必须能够处理身份验证(用户是谁)和授权(用户可以做什么)这两大问题。
建立身份验证和授权机制本身就十分困难,要确保正确则更加困难,而这直接关系到您的产品能否取得成功。
Firebase Authentication 让您能够轻松验证用户身份,与之类似,面向 Cloud Storage 的 Firebase Security Rules 安全规则可以为您简化向用户授权和验证请求的工作。Cloud Storage Security Rules 允许您指定基于路径的权限,降低了复杂性。只需几行代码,您就可以编写出授权规则,用于限定能够发出 Cloud Storage 请求的用户身份,或限制上传文件的大小。
Firebase Realtime Database 具有类似的功能,称为 Firebase Realtime Database Security Rules
身份验证
了解您的用户是谁,这是构建应用时需要考虑的一个重要方面,Firebase Authentication 提供了一种易于使用且安全的纯客户端侧身份验证解决方案。面向 Cloud Storage 的 Firebase Security Rules 与 Firebase Authentication 相结合,可以实现基于用户的安全性。当使用 Firebase Authentication 对某个用户进行了身份验证后,Cloud Storage Security Rules 中的 request.auth
变量会变成一个包含该用户唯一 ID (request.auth.uid
) 和令牌 (request.auth.token
) 中所有其他用户信息的对象。当用户未通过身份验证时,request.auth
则为 null
。这可以让您针对每个用户安全地控制数据访问。如需了解详情,请参阅身份验证部分。
授权
识别用户只是保证安全的一部分。在知道用户的身份后,您需要一种方法来控制该用户对 Cloud Storage 中的文件的访问权限。
Cloud Storage 允许您指定基于文件和路径的授权规则(这些规则驻留在服务器上),以及确定对您应用中的文件的访问权限。例如,默认的 Cloud Storage Security Rules 安全规则需要借助 Firebase Authentication 才能对所有文件执行任何 read
或 write
操作:
service firebase.storage {
match /b/{bucket}/o {
match /someFolder/{fileName} {
allow read, write: if request.auth != null;
}
}
}
您可以前往 Firebase 控制台选择一个 Firebase 应用并查看“Storage”部分的 Rules
标签页,来修改这些规则。
数据验证
面向 Cloud Storage 的 Firebase Security Rules 也可用于数据验证,包括验证文件名和路径以及文件元数据属性(例如 contentType
和 size
)。
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
// Only allow uploads of any image file that's less than 5MB
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
}
}
}
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-12。
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],null,["\u003cbr /\u003e\n\nTraditionally, security has been one of the most complex parts of app\ndevelopment. In most applications, developers must build and run a server that\nhandles authentication (who a user is) and authorization (what a user can do).\nAuthentication and authorization are hard to set up, harder to get right, and\ncritical to the success of your product.\n\nSimilar to how Firebase Authentication makes it easy for you to authenticate your\nusers, Firebase Security Rules for Cloud Storage makes it easy for you to authorize users\nand validate requests. Cloud Storage Security Rules manage the complexity for you by\nallowing you to specify path based permissions. In just a few lines of code, you\ncan write authorization rules that restrict Cloud Storage requests to a\ncertain user or limit the size of an upload.\n| **Note:** If you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nThe Firebase Realtime Database has a similar feature, called\n[Firebase Realtime Database Security Rules](/docs/database/security)\n\nAuthentication\n\nKnowing who your users are is an important part of building an application, and\nFirebase Authentication provides an easy to use, secure, client side only solution\nto authentication. Firebase Security Rules for Cloud Storage ties in to Firebase Authentication\nfor user based security. When a user is authenticated with Firebase Authentication,\nthe `request.auth` variable in Cloud Storage Security Rules becomes an object that\ncontains the user's unique ID (`request.auth.uid`) and all other user\ninformation in the token (`request.auth.token`). When the user is not\nauthenticated, `request.auth` is `null`. This allows you to securely control\ndata access on a per-user basis. You can learn more in the\n[Authentication](/docs/storage/security/rules-conditions#authentication) section.\n\nAuthorization\n\nIdentifying your user is only part of security. Once you know who they are, you\nneed a way to control their access to files in Cloud Storage.\n\nCloud Storage lets you specify per file and per path authorization\nrules that live on our servers and determine access to the files in your app.\nFor example, the default Cloud Storage Security Rules require Firebase Authentication in\norder to perform any `read` or `write` operations on all files: \n\n```css+lasso\nservice firebase.storage {\n match /b/{bucket}/o {\n match /someFolder/{fileName} {\n allow read, write: if request.auth != null;\n }\n }\n}\n```\n\nYou can edit these rules by selecting a Firebase app in the [Firebase console](//console.firebase.google.com/)\nand viewing the `Rules` tab of the Storage section.\n\nData Validation\n\nFirebase Security Rules for Cloud Storage can also be used for data validation, including\nvalidating file name and path as well as file metadata properties such as\n`contentType` and `size`. \n\n```gdscript\nservice firebase.storage {\n match /b/{bucket}/o {\n match /images/{imageId} {\n // Only allow uploads of any image file that's less than 5MB\n allow write: if request.resource.size \u003c 5 * 1024 * 1024\n && request.resource.contentType.matches('image/.*');\n }\n }\n}\n```\n\nNext steps\n\n- [Get started](/docs/storage/security/get-started) planning rules development\n for your Cloud Storage buckets.\n\n- Learn more about [securing your data](/docs/storage/security/core-syntax)\n using security rules."]]