Firebase存储:使用http url路径和自定义访问令牌下载受访问保护的映像
我在通过url和自定义访问令牌下载受访问保护的映像时遇到问题 身份验证错误:Firebase存储:使用http url路径和自定义访问令牌下载受访问保护的映像,firebase,firebase-authentication,firebase-storage,Firebase,Firebase Authentication,Firebase Storage,我在通过url和自定义访问令牌下载受访问保护的映像时遇到问题 身份验证错误: { "error": { "code": 403, "message": "Permission denied. Could not perform this operation" } } 我是否缺少一些配置?? 我用于访问以自定义令牌作为查询参数的图像的Http url: https://myfirebasestorage.googleapis.com/v0/b/myfireba
{
"error": {
"code": 403,
"message": "Permission denied. Could not perform this operation"
}
}
我是否缺少一些配置??
我用于访问以自定义令牌作为查询参数的图像的Http url:
https://myfirebasestorage.googleapis.com/v0/b/myfirebasestorage-my_proj_id.appspot.com/o/images%2FIMG_4138.JPG?alt=media&token=TOKEN_GOT_USING_CREATE_CUSTOM_TOKEN_SIGN_IN
我的存储规则:
service firebase.storage {
match /b/myfirebasestorage-my_proj_id.appspot.com/o {
match /{allPaths=**} {
allow read: if request.auth != null;
}
}
}
自定义令牌在java应用程序中生成,如下所示:
public static String createCustomToken(String userId, Map<String, Object> additionalClaims) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(new FileInputStream(FIREBASE_ACCESS_FILE)).build();
FirebaseApp.initializeApp(options);
Task<String> customToken = FirebaseAuth.getInstance().createCustomToken(userId, additionalClaims);
return customToken.getResult().toString();
}
VerifiedToken=>uid: user_id_1,
email: bhaarat@new.mail,
additionalClaims: {
"aud": "myfirebasestorage-<my_proj_id>",
"auth_time": 1480609782,
"email": "bhaarat@new.mail",
"email_verified": false,
"exp": 1480621773,
"iat": 1480618173,
"iss": "https://securetoken.google.com/myfirebasestorage-my_proj_id",
"sub": "user_id_1",
"circleId": "circle_id_1",
"memberId": "user_id_1",
"user_id": "user_id_1",
"firebase": {
"identities": {
},
"sign_in_provider": "custom”
}
}
我的数据库规则
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
简而言之,
存储
使用的?令牌=
与实时数据库
使用的?auth=
不同(因此它们的名称不同)
存储器提供了格式为https://myfirebasestorage.googleapis.com/v0/b//o/?alt=media&token=
这是为与不使用Firebase身份验证的用户共享而设计的(想象一下与家人/朋友共享照片,但你不想让他们下载应用程序来实现这一点)
如果您想让下载受到存储安全规则的保护,您需要使用原生Android方法
getBytes()
,getStream
,getFile()
,等等。您可以尝试为所有请求设置规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
我认为令牌也适用于Firebase存储,而不仅仅是本页标题“用户专用”或“组专用”下提到的Firebase数据库。请看一看。我知道存储提供了一个不可用的下载URL,您也可以直接从firebase控制台->对象属性->下载URL中检索。问题是,一旦有人知道下载url,就没有基于用户的访问保护:任何人都可以查看内容,因此没有办法保护它。我的使用案例如下=>只要你在我的朋友组中,我想与你共享我的视频。一旦你离开我的好友群,我将不想与你共享任何内容,并将内容仅限于同一群中的其他好友。但在这种情况下,即使你不再是我的朋友组的一部分,并且因为你已经获得了视频url,你也可以继续访问该视频。
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}