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了。

  • 相关阅读:
    goland 安装包激活码
    go目录文件
    php 单表数据转化为json格式
    thinkphp分页查询,后台处理怎么做
    用js 获取url 参数 页面跳转 ? 后的参数
    约瑟夫问题
    openstack配置
    Python数据结构
    Python常用排序算法
    scrapy使用
  • 原文地址:https://www.cnblogs.com/bestzhang/p/6127886.html
Copyright © 2011-2022 走看看