zoukankan      html  css  js  c++  java
  • springMVC文件从Nginx下载文件权限控制

    思路:

      文件下载请求 到 后台;后台判断权限,不通过则不下载;通过则设置 X-Accel-Redirect;Nginx获取“X-Accel-Redirect”后以sendfile方式从NFS读取文件并进行下载

    优点:

    • 不会直接暴露文件地址 抓包工具不会抓到地址;
    • 可以控制权限;

    后台示例代码:

     1 @RequestMapping(value = "/offline", method = RequestMethod.GET)
     2     public void doDownloadOffline(HttpServletResponse response) throws IOException {
     3 
     4         File zipFile = new File("/Users/lixiuming/Desktop/test.json");
     5         if (zipFile == null || !zipFile.exists()) {
     6             response.sendError(404);
     7         }
     8         response.setHeader("Content-Type", "application/octet-stream");
     9         // 设置转发属性
    10         // /appoffline/为Nginx location 名
    11         response.setHeader("X-Accel-Redirect", "/appoffline/" + zipFile.getName());
    12         response.setHeader("X-Accel-Charset", "utf-8");
    13         response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
    14     }

    传参示例:

     1 @RequestMapping(value = "/offline", method = RequestMethod.GET)
     2     public void doDownloadOffline(Integer file_id, HttpServletResponse response) throws IOException {
     3         if (file_id != null && file_id != 0) {
     4             File zipFile = new File("/Users/lixiuming/Desktop/test.json");
     5             if (zipFile == null || !zipFile.exists()) {
     6                 response.sendError(404);
     7             }
     8             response.setHeader("Content-Type", "application/octet-stream");
     9             // 设置转发属性
    10             // /appoffline/为Nginx location 名
    11             response.setHeader("X-Accel-Redirect", "/appoffline/" + zipFile.getName());
    12             response.setHeader("X-Accel-Charset", "utf-8");
    13             response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
    14         } else {
    15             System.out.println("error");
    16         }
    17 
    18     }
    View Code

    说明:

    • /appoffline/为Nginx location 名;
    • 这里的  @RequestMapping(value = "/offline", method = RequestMethod.GET),/offline;当监听到 Nginx监听到 /download_file时,访问了后台(地址是/offline)

    nginx.conf配置代码:

     1   location  / {
     2             root   html;
     3             proxy_pass https://www.baidu.com;
     4             index  index.html index.htm;
     5         }
     6 
     7            location = /download_file {
     8             proxy_pass http://127.0.0.1:8080/offline;
     9         }
    10            location /appoffline/ {
    11                 #设置非浏览器访问
    12                 internal;
    13                 charset utf-8;
    14                 alias /Users/lixiuming/Desktop/;
    15         }

     说明:当访问 http://localhost:8081/download_file?file_id=1 (不传参地址http://localhost:8081/download_file) 时,可以执行下载文件,F12 NETWORK 没有文件地址;

    我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。 我要做一个自由又自律的人,靠势必实现的决心认真地活着。
  • 相关阅读:
    发布 Rafy .NET Standard 版本 Nuget 包
    使用 MarkDown & DocFX 升级 Rafy 帮助文档
    apache2服务器支持cgi功能
    百兆网口与千兆网口速率协商不成功
    ubuntu etho0 up cron
    linux 后台进程
    MySQL的事务性
    linux下visual studio code配置c++调试环境实例
    linux下visual studio code中gdb调试文件launch.json解析
    Zookeeper安装后,编译C client时报错"syntax error near unexpected token `1.10.2"
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/15196421.html
Copyright © 2011-2022 走看看