Ruby on rails 4 Rails图像上传安全性
目前,我采用Carrierwave为用户提供图像 然而,我几乎找不到图像安全的解决方案,即如何为上传的图像设置图像授权,以仅允许同一组中的特定用户查看 在挖掘了Facebook的实现之后,我观察到他们将这些参数(哦,oe,uuuu gda_u)注入到图像url中Ruby on rails 4 Rails图像上传安全性,ruby-on-rails-4,paperclip,carrierwave,image-uploading,dropzone.js,Ruby On Rails 4,Paperclip,Carrierwave,Image Uploading,Dropzone.js,目前,我采用Carrierwave为用户提供图像 然而,我几乎找不到图像安全的解决方案,即如何为上传的图像设置图像授权,以仅允许同一组中的特定用户查看 在挖掘了Facebook的实现之后,我观察到他们将这些参数(哦,oe,uuuu gda_u)注入到图像url中 oh=924eb34394&oe=55E07&Uuu gda=14363393492FC8BF91E1AEC5 对于carrierwave或回形针是否有类似的实现 谢谢我用这个做了不少工作(只用回形针) 有一个解决方案是可以的,但它
oh=924eb34394&oe=55E07&Uuu gda=14363393492FC8BF91E1AEC5
对于carrierwave或回形针是否有类似的实现
谢谢我用这个做了不少工作(只用回形针) 有一个解决方案是可以的,但它需要大量的处理 如果您只想隐藏文件,不让文件循环,您可以散列回形针附件,请参见以下内容: 如果要在每次图像加载时授权用户,可以执行以下操作: 将文件移出公用文件夹
has_attached_file :image,
styles: { large: '1500x1500>', small: '250x250>'},
path: ':rails_root/storage/gallery/image/:style/:filename'
使用Sendfile查看您的文件
def show
send_file(object.image.path(:small), filename: object.image_file_name, type: "image/png",disposition: 'inline',x_sendfile: true)
end
但是,我有点不愿意实现这个功能,例如图像库,因为它需要对每个图像执行GET
-action+授权。使用x-sendfile
与Apache配合使用,可以更快地提供图像
参考:
我从一家公司找到了这个很好的回形针解决方案 虽然有点过时,但本文详细介绍了保护对附件的访问以及如何保护文件本身所需的一切。本文描述了实现它的所有步骤,包括Capistrano部署 请确保通过更改以下内容来使用更新的路线:
map.resources :notes, :member => { :attachment => :get }
致:
我还更新了以下链接:
link_to 'Download attachment', [:attachment, @note]
致:
另请参阅以了解如何配置url。默认情况下,Carrierwave将上载存储在
/public
中,其中所有内容仅作为静态内容。如果您需要控制对此上载的访问,我将首先配置一个不同的存储路径
class TestUploader < CarrierWave::Uploader::Base
def store_dir
Rails.root.join('uploads', relative_path).to_s
end
def serving_path # Use this method to get the serving path of the upload
File.join '/uploads', relative_path
end
private
def relative_path
File.join model.class.model_name.plural, model.id.to_s
end
end
类TestUploaderclass Test < ApplicationRecord
mount_uploader :file, TestUploader
end
Rails.application.routes.draw do
get '/uploads/:model/:id', to: 'uploads#get'
end
class UploadsController < ApplicationController
def get
# ... autorization logic
model = params.fetch(:model).singularize.camelcase.safe_constantize
return head 400 unless model.present?
send_file model.find(params.fetch(:id)).file.path
end
end
类测试
class TestUploader < CarrierWave::Uploader::Base
def store_dir
Rails.root.join('uploads', relative_path).to_s
end
def serving_path # Use this method to get the serving path of the upload
File.join '/uploads', relative_path
end
private
def relative_path
File.join model.class.model_name.plural, model.id.to_s
end
end
class Test < ApplicationRecord
mount_uploader :file, TestUploader
end
Rails.application.routes.draw do
get '/uploads/:model/:id', to: 'uploads#get'
end
class UploadsController < ApplicationController
def get
# ... autorization logic
model = params.fetch(:model).singularize.camelcase.safe_constantize
return head 400 unless model.present?
send_file model.find(params.fetch(:id)).file.path
end
end