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

  • 相关阅读:
    FastJson---高性能JSON开发包
    mybatis中大于等于小于等于的写法
    MarkDown 使用说明示例
    Get和Post的参数传值
    规则引擎 资料收集
    ORA-01033错误解决方案
    mybatis 参数为String,用_parameter 取值
    php中实现记住密码下次自动登录的例子
    php 应用 bootstrap-fileinput 上传文件 插件 操作的方法
    AJAX 跨域请求
  • 原文地址:https://www.cnblogs.com/bestzhang/p/6127886.html
Copyright © 2011-2022 走看看