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

发布于:2025-04-21 ⋅ 阅读:(40) ⋅ 点赞:(0)

前言:系统有一些文件是通过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,在访问此文件,则加载正常。


网站公告

今日签到

点亮在社区的每一天
去签到