文件下载

文件安全技术方案

问题提出a

公司需要向用户提供下载文件的接口。
如果直接向用户提供url,那么用户通过修改Url可以拿到别的用户的文件。
如果这份文件是关于用户账户的数据,
那么,这实在是太可怕了?

原则

  • 就是不要把 后端存储 直接暴露出去,不能让 客户端知道你的 文件服务器的 ip
  • 权限控制

怎么办

如果通过url提供用户下载,那么,通过猜的方法,理论上,肯定可以拿到别人的文件。
所以,需要从这方面做文章。

不能让用户知道你的文件路径url。
直接输出文件的二进制流,带个http response body当中。

伪代码如下:

@RequestMapping("user/file/{file_index}")
public HttpServletResponse(int file_index,HttpServletResponse response){
    user_id = userService.getUserId();
    user_file = fileService.getFileByUserId(user_id,file_index);
    String file = FileUtils.getBody(user_file);
 
    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
    response.addHeader("Content-Length", "" + file.length());
    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/octet-stream");
    toClient.write(buffer);
    toClient.flush();
    toClient.close();

    return response;
}

优缺点

优点

  • 安全

缺点

  • 性能差

结论

如果能接受慢,性能差的缺点,那就OK了。

posted @ 2016-12-03 03:00  我的天啊~  阅读(182)  评论(0编辑  收藏  举报