【问题标题】:Firebase avoid illegal access to StorageFirebase 避免非法访问存储
【发布时间】:2017-09-25 13:25:17
【问题描述】:

我正在构建一个 Android 应用,目前正在集成 Google Firebase。我已经建立了一个数据库。由于我的应用程序将通过 Google Firebase 存储下载敏感图像,因此只能允许授权用户访问这些图像。在 Firebase 数据库中,我手动添加了存储中图像的 URL(我希望自动添加,但我不知道如何操作)。

让不同用户访问 Firebase 存储中不同图形的最佳方式是什么?

谢谢!

编辑:我已经读过:Google Firebase,但以难以猜测的名称重命名图像,似乎不是最安全的解决方案

【问题讨论】:

  • 使用随机生成的 base-64 名称长度为 30 的图像重命名将需要 64^30 或 10^54 次尝试进行暴力攻击。这是一个非常庞大的数字。除此之外,您在文档中看到您可以从基于火的控制台以类似 JSON 的格式定义安全标准。

标签: android security firebase firebase-storage


【解决方案1】:

是用户上传图片还是您通过应用上传? 如果是这样,我可以建议:

有一个私人图像按钮。

在 onCreate() 中

 imageButton =  (ImageButton) findViewById(R.id.ibImageSelect);
imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            startActivityForResult(galleryIntent, Gallery_Request);

        }
    });

在方法体之外:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Gallery_Request && resultCode == RESULT_OK) {
        imageUri = data.getData();
        imageButton.setImageURI(imageUri);
    }
}

点击按钮

StorageReference upload = storageReference.child("what you want");
Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
 byte[] data = baos.toByteArray();
    UploadTask filePath = upload.child(imageUri.getLastPathSegment()).putBytes(data);
 filePath.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

 filePath.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

@Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


     DatabaseReference newPost = mref.push(); //this generates a random unique key 
replace with your own if you want, since you already got your own structure



    newPost.child("image").setValue(imageUri.getLastPathSegment()); 
//set image name so can use this to download the image

    }
     });

您可以在数据的每个节点中有一个“状态”子节点,然后在下载图像时遍历所有节点,检查状态是否是您想要的状态。然后获取图片下载密钥。

用于下载图片:

遍历节点和子节点,获取img url,

    String ref = "image folder in firebase storage/" + imgurl;
                storageRef.child(ref).getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                    @Override
                    public void onSuccess(byte[] bytes) {
                        // Use the bytes to display the image
                          final  Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView iv = (ImageView) findViewByID(R.id...);

iv.setImageBitmap(bitmap);



                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.i("error", exception.getMessage());
                    }
                });

【讨论】:

  • 感谢您的回答。但是,此解决方案无法阻止允许黑客猜测文件名的暴力攻击。您的解决方案不保护尚未未经授权的访问数据。还是我错了?
  • 编辑:20 GB/月的价格是数据库的 256 倍,是存储的 256 倍,所以我肯定更喜欢使用存储。 :) firebase.google.com/pricing
猜你喜欢
  • 2022-01-09
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-20
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 2018-11-27
相关资源
最近更新 更多