0

我正在尝试制作一个在线照片库,用户可以将图像从他们的移动设备(如 Android)上传到服务器并可以访问它。目前我正在使用带有 Spring Security Plugin Core 的 Grails,并将所有上传的图像保存在 /web-app/uploaded-image 的文件夹中。上传部分目前正在工作,我可以将图像从我的设备上传到服务器上传的图像文件夹。但是,我的一个问题是,目前我在 Config.groovy 中使用这些行来执行访问控制部分

grails.plugins.springsecurity.interceptUrlMap = [
/uploaded_image/**':['IS_AUTHENTICATED_REMEMBERED']
]

我计划让控制器将图像的路径与称为图像的 DomainClass 相关联,该图像将与每个用户的 DomainClass 相关联。我的问题是,我如何才能使不是图像所有者的用户无权查看图像?我尝试在谷歌搜索答案并没有找到很多有用的答案。感谢您的时间。

4

2 回答 2

1

首先,将图像上传到分解的 WAR 路径是一个坏主意。当您必须部署更新时会发生什么?最好将图像完全存储在 WAR 结构之外。

其次,我用几种不同的方式处理了这个问题。

  • 通过 Grails 控制器将图像流回。这样您就可以直接访问图像请求并处理您想要的任何类型的安全性,包括检查角色等。

非常简单的版本,不考虑缓存、响应标头等:

def avatarFilePath = new File(userInstance.avatarURL)
response.setContentType("application/png")
response.setContentLength(avatarFilePath.size().toInteger())
OutputStream out = response.getOutputStream();
out.write(avatarFilePath.bytes);
out.close();
  • 将图像存储在某个内容存储库(可能是 AmazonS3)中,然后通过提供程序 API 控制权限,或者让您自己的控制器管理安全性,然后通过提供程序进行适当的重定向。

  • 在您的 Tomcat/Container 前面使用 Apache,并让 Apache 处理将图像流回的所有细节。但是,我不确定在这种情况下如何处理安全性。

于 2012-04-06T11:49:36.187 回答
0

您可以使用以下逻辑来确保不是图像所有者的用户无权查看图像

class ImageController {


  def springSecurityService

  def showImage() {
  Image image=Image.get(parmas.long("id")

 // this is the user  who is trying to access image from ui
    User user = User.get(image.userId) 

 //this is the logged-in user       
  User logged = User.get(springSecurityService.principal.id)
   if (user.id != logged.id) {
  {
      redirect(action: "accessDenied", controller='access' id: params.long("id"))        
    //re-direct accessDenied page

 }else{
 //show image  
  }
  }

  Class AccessController{
 def accessDenied= {

    render(view: "accessDenied")

}
}
于 2012-04-06T20:36:03.137 回答