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 名 还可配置限速相关// 限速控制 response.setHeader("X-Accel-Limit-Rate", String.valueOf((k * 1024)));
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 }
传参示例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
说明:
- /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 没有文件地址;
其他配置:root/alias/proxy_pass
root/alias 是指定文件路径的两种方式,主要区别就是怎么解析location后面的uri。 eg: 访问:http://localhost/appImg/abc.jpg
-
root:
location ^~ /appImg/{
root /home/nginx;
}
这个location相当于访问服务器上的文件路径: /home/nginx/appImg/abc.jpg 。
-
alias:
location ^~ /appImg/{
alias /home/nginx/;
}
这个location相当于访问服务器上的文件目录:/home/nginx/abc.jpg(即alias不会使用location后面配置的路径)。而且如果alias 指定的是目录,后面一定要加上 "/"。。。
-
proxy_pass
补充nginx的常用命令:
Linux:
帮助命令:nginx -h
启动Nginx服务器 :sudo nginx
查看进程: ps aux | grep nginx
配置文件路径:/usr/local/nginx/conf/nginx.conf
检查配置文件:sudo nginx -t
指定启动配置文件:sudo nginx -c /usr/local/nginx/conf/nginx.conf
暴力停止服务:sudo nginx -s stop
优雅停止服务:sudo nginx -s quit
重新加载配置文件:sudo nginx -s reload
windows:
帮助命令:nginx -h
启动Nginx服务器 start nginx或nginx.exe
停止:nginx.exe -s stop或nginx.exe -s quit
重新载入Nginx:nginx.exe -s reload
重新打开日志文件:nginx.exe -s reopen
查看版本:nginx -v
查看nginx 是否启动 tasklist /fi "imagename eq nginx.exe"(结束进程taskkill /F -pid 25428)
参考配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 server { 2 listen 9000; 3 server_name 192.168.1.111;#web服务当前地址 4 autoindex on; 5 6 location ^~ /self-img/ { 7 root D:\\aa\\bb\\cc\\dd; 8 add_header Access-Control-Allow-Origin *; 9 } 10 location ^~ /codeimg/ { 11 root D:\\aa\\bb\\cc\\; 12 add_header Access-Control-Allow-Origin *; 13 } 14 location ^~ /apk/ { 15 root D:\\aa\\bb\\cc\\temp; 16 add_header Access-Control-Allow-Origin *; 17 } 18 location / { 19 alias D:/aa/bb/cc/filelist/; 20 add_header Access-Control-Allow-Origin *; 21 } 22 23 location = /download_file { 24 proxy_pass http://115.239.228.111:49155/down.qq.com/warthunder/full/W/0.0.6.5/WT_W_V0.0.6.5_full.exe; 25 } 26 location /appoffline/ { 27 #设置非浏览器访问 28 internal; 29 charset utf-8; 30 alias D:/aa/bb/cc/dd/; 31 } 32 33 error_page 500 502 503 504 /50x.html; 34 location = /50x.html { 35 root html; 36 } 37 }