当前位置:   article > 正文

Nginx做静态文件服务器,如何进行权限验证呢?_nginx 访问文件添加验证

nginx 访问文件添加验证

前言

在我们的日常开发过程中,经常使用nginx做文件读取服务器,因为配置非常简单,方便使用。只要通过IP和端口加上文件路径就可以读到文件或者图片了。但是,我们的安全问题该如何处理?并不是所有的人拿到图片路径就可以访问文件,这样很有可能造成文件泄露。

因此,我们想的是,在通过路径获取文件的时候,可以携带token信息,通过我们的系统服务进行token验证,如果token合法,才能成功获取图片,否则拒绝此次请求。

以下是具体的实现方式,通过Nginx的auth_request模块

1.配置Nginx静态服务器

下载nginx,解压之后,打开conf文件夹下面的nginx.conf
设置静态文件路径,然后在根目录执行nginx启动,静态文件服务器就可以使用了

  1. server {
  2. listen 8088;
  3. server_name 127.0.0.1;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. alias D:/work/file/;
  8. index index.html index.htm;
  9. }
  10. #error_page 404 /404.html;
  11. # redirect server error pages to the static page /50x.html
  12. #
  13. error_page 500 502 503 504 /50x.html;
  14. location = /50x.html {
  15. root html;
  16. }
  17. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18. #
  19. #location ~ \.php$ {
  20. # proxy_pass http://127.0.0.1;
  21. #}
  22. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23. #
  24. #location ~ \.php$ {
  25. # root html;
  26. # fastcgi_pass 127.0.0.1:9000;
  27. # fastcgi_index index.php;
  28. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  29. # include fastcgi_params;
  30. #}
  31. # deny access to .htaccess files, if Apache's document root
  32. # concurs with nginx's one
  33. #
  34. #location ~ /\.ht {
  35. # deny all;
  36. #}
  37. }

 

文件夹下放了一张图片,我们打开浏览器,输入http://127.0.0.1/cat.jpeg就可以访问到了

2.编写后台授权接口

正常我们都是通过hearder中携带授权token信息,所以我们后台写个接口,通过HttpServletRequest获取header中的token信息,再进行业务的验证就可以了,auth_request模块是根据返回的http状态值来判断是否通过授权,200则为成功,401或者403为授权失败

  1. @RequestMapping("/authFileValid")
  2. @ResponseBody
  3. public void authFileValid(HttpServletRequest request,HttpServletResponse response){
  4. String token = request.getHeader("accessToken");
  5. System.out.println("获取的token:"+token);
  6. if(token != null){
  7. //验证token是否合法
  8. }else{
  9. response.setStatus(HttpStatus.UNAUTHORIZED.value());
  10. }
  11. }

3.修改nginx配置文件

  1. server {
  2. listen 8088;
  3. server_name 127.0.0.1;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. alias D:/work/file/;
  8. # 设置鉴权的请求
  9. auth_request /authFileValid;
  10. # 从查询参数中获取 token,并赋值给token变量
  11. set $token $arg_token;
  12. # 自定义验证失败时的处理页面
  13. error_page 401 = /auth-required;
  14. }
  15. location = /authFileValid {
  16. internal; # 只允许内部访问
  17. proxy_pass http://127.0.0.1:8080/authFileValid;
  18. proxy_pass_request_body off;
  19. proxy_set_header Content-Length "";
  20. proxy_set_header X-Original-URI $request_uri;
  21. # 设置AccessToken 的值为token
  22. proxy_set_header AccessToken "$token";
  23. }
  24. location = /auth-required {
  25. return 401; # 返回 401 状态码
  26. }
  27. #error_page 404 /404.html;
  28. # redirect server error pages to the static page /50x.html
  29. #
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  35. #
  36. #location ~ \.php$ {
  37. # proxy_pass http://127.0.0.1;
  38. #}
  39. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  40. #
  41. #location ~ \.php$ {
  42. # root html;
  43. # fastcgi_pass 127.0.0.1:9000;
  44. # fastcgi_index index.php;
  45. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  46. # include fastcgi_params;
  47. #}
  48. # deny access to .htaccess files, if Apache's document root
  49. # concurs with nginx's one
  50. #
  51. #location ~ /\.ht {
  52. # deny all;
  53. #}
  54. }

注意

 set $token $arg_token中的arg_是参数前缀固定写法,实则是获取的查询参数中的token值

例如http://127.0.0.1:8088/cat.jpeg?token=xxxxx

 4.测试

重新启动nginx,启动后台web,浏览器访问http://127.0.0.1:8088/cat.jpeg,就可以看到下面的结果了

前端:

后台:

我们可以看到,再次访问图片返回了401,这时候我们已经没有权限去访问图片了

这次我们随便设置一下token值,后台并没有进行验证token的正确性,便于测试只是验证了非空

前端:

后端:

 如此我们便实现了nginx调用后台接口授权的整个流程 

学习更多简单好理解的编程和架构知识!
关注我 不迷路

或者微信 添加公众号,发送NginxAuth获取教程源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/293253
推荐阅读
  

闽ICP备14008679号