当前位置: 首页 > news >正文

Nginx静态资源跳转添加权限验证

前言:系统有一些文件是通过nginx静态资源转发直接访问文件服务器进行访问,有信息泄露风险,因此需要增加登录验证。由于之前的文件预览都是前端通过a标签直接跳转到文件目录,逐一修改预览功能耗时较多,因此在修改nginx配置文件来实现此功能。

通过浏览器直接打开此路径,可以直接打开目标图片,需要修改为只有登录过的用户才可以打开图片。

http://your_ip:8040/staticfile/userfiles/XAGJGDYSB20250407Y531/CE202504011343046138/TEMP1111111/attachment/1/1167610aa17b0813233fe82d99403e41.jpg

修改思路:1.前端在登录之后将token存储在cookies里

                   2.在后台增加一个验证登陆状态的接口,返回特定状态码

                   3.修改nginx配置文件,静态文件跳转时先调用状态验证接口

一、前端修改

在登陆成功后,往cookies里set值,注销登录,删除掉。

验证:在下图可以看到此token,说明前端工作完成了。

需要注意的是,cookies最大4K,token不能超过,不然会设置不上。

二、后端接口修改

@AnonAccess
@GetMapping("/isLogin")
public ResponseEntity<?> isLogin() {boolean isLogin = StringUtils.isNotBlank(UserUtils.getUserId());log.info("=======================isLogin:{}", isLogin);if (isLogin) {return ResponseEntity.ok().build();} else {return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();}
}

新增上述接口,isLogin是自己项目里用于判断是否登录的接口。

验证:不带token访问此接口,返回401,携带token访问此接口,返回200

三、nginx配置文件修改

代码:

location /staticfile/ {
            auth_request /auth-validate/;
            auth_request_set $auth_status $upstream_status;
            alias /mnt/hd1/isptest/software/40_apache-tomcat-8.0.44/webapps/ROOT/;
            error_page 401 = @error401;
            error_page 403 = @error403;
            add_header 'Access-Control-Allow-Origin' '*' always;
           add_header 'Access-Control-Allow-Methods' 'GET' always;
           add_header 'Access-Control-Allow-Headers' 'Content-Type, Authorization, X-Requested-With' always;
           add_header 'Access-Control-Allow-Credentials' 'true' always;
        }    
       location  /auth-validate/ {
           proxy_pass http://127.0.0.1:8020/auth/isLogin;
           proxy_set_header Cookie $http_cookie;
          # 必须传递的headers
          proxy_set_header Host $host;
          proxy_set_header X-Original-URI $request_uri;
          proxy_set_header X-Real-IP $remote_addr;
          # 清除不需要的headers
          proxy_pass_request_body off;
          proxy_set_header Content-Length "";
        
         # 从Cookie中提取JWT
         set $jwt "";
         set $auth_header ""; 
         if ($http_cookie ~* "token=([^;]+)(?:;|$)") {
               set $jwt $1;
               set $auth_header "Bearer $jwt";  # 仅在提取到 token 时设置
         }
         proxy_set_header Authorization $auth_header;
    }
    

        # 错误处理
    location @error401 {
        return 401 '{"code":401,"message":"Unauthorized"}';
    }

    location @error403 {
        return 403 '{"code":403,"message":"Forbidden"}';
    }

原来转发就是直接根据识别的路径,转发到对应的文件目录,改为先调用权限认证接口,若接口返回401,403则不能访问。

注意事项:

1.如果修改后重启nginx报错,可能是缺少http_auth_request_module模块,需要给nginx安装http_auth_request_module模块。

配置nginx,需要支持http_auth_request_module
    找到nginx初始压缩包目录 
    nginx -V 查看原有配置参数,添加--with-http_auth_request_module
    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_auth_request_module
    make
    sudo make install
    ./nginx -V 检查有没有 with-http_auth_request_module
    重启nginx(先停后启,不要reload)

四、效果展示

1.cookies没有token,访问静态资源无法加载

2.在新的tab页登录账号,确保浏览器里有cookies,在访问此文件,则加载正常。

http://www.xdnf.cn/news/54883.html

相关文章:

  • 不确定与非单调推理的模糊推理
  • c语言修炼秘籍 - - 禁(进)忌(阶)秘(技)术(巧)【第六式】文件操作
  • System.out 详解
  • 东京 ⇄ 京都游记⛩️
  • Linux 420 find stat touch tree scp crontab
  • 【Android】Wallpaper学习
  • cpp知识章节
  • 除了`String`、`StringBuffer` 和 `StringBuilder`之外,还有什么处理字符串的方法?
  • 使用DeepSeek的AIGC的内容创作者,如何看待陈望道先生所著的《修辞学发凡》?
  • 从外网访问局域网服务器的方法+Linux文件和命令
  • ONLYOFFICE深度解锁系列.3-OnlyOffice集成第三方软件原理揭秘:如何提高文件打开速度
  • 基于一致性哈希算法原理和分布式系统容错机制
  • C++编程 希尔排序
  • 状态管理最佳实践:Provider使用技巧与源码分析
  • 【C语言函数部分的重要知识点】--自定义函数,static和extern
  • 【题解-JSOI】JSOI2009 配菜
  • 【连接池-55.1】深入解析Druid连接池:高性能Java数据库连接池的最佳实践
  • Python 爬虫案例
  • Dubbo QoS操作手册
  • Spring 01
  • 前端与传统接口的桥梁:JSONP解决方案
  • 大数定理(LLN)习题集 · 答案与解析篇
  • QCPAxis、QCPGrid 和 QCPAxisTicker 三者关系
  • 关于隔离2:ADC芯片
  • 京东 h5st 5.1 详情 京东滑块 cfe 分析
  • Cursor工具你会用了吗
  • leetcode0078. 子集-medium
  • stm32 13位时间戳转换为时间格式、对时
  • Day58 | 179. 最大数、316. 去除重复字母、334. 递增的三元子序列
  • Linux系统的远程终端登录、远程图形桌面访问、 X图形窗口访问