zoukankan      html  css  js  c++  java
  • 文件下载

    文件安全技术方案

    问题提出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了。

  • 相关阅读:
    js中return;、return true、return false;区别
    JS跨域设置和取Cookie
    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(转)
    IIS7中Ajax.AjaxMethod无效的原因及解决方法
    ajax 跨域的问题 用js绕过跨域
    微服务笔记
    smali语法笔记
    Go Micro 入门笔记
    介绍微服务框架Micro笔记
    JS任务队列--笔记
  • 原文地址:https://www.cnblogs.com/bestzhang/p/6127886.html
Copyright © 2011-2022 走看看