欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Nginx做靜態(tài)文件服務(wù)器,如何進(jìn)行權(quán)限驗(yàn)證

 更新時(shí)間:2024年06月19日 14:57:55   作者:程序員的小黑板  
這篇文章主要介紹了使用Nginx做靜態(tài)文件服務(wù)器,如何進(jìn)行權(quán)限驗(yàn)證問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

在我們的日常開發(fā)過程中,經(jīng)常使用nginx做文件讀取服務(wù)器,因?yàn)榕渲梅浅:唵?,方便使用。只要通過IP和端口加上文件路徑就可以讀到文件或者圖片了。但是,我們的安全問題該如何處理?并不是所有的人拿到圖片路徑就可以訪問文件,這樣很有可能造成文件泄露。

因此,我們想的是,在通過路徑獲取文件的時(shí)候,可以攜帶token信息,通過我們的系統(tǒng)服務(wù)進(jìn)行token驗(yàn)證,如果token合法,才能成功獲取圖片,否則拒絕此次請求。

以下是具體的實(shí)現(xiàn)方式,通過Nginx的auth_request模塊

1.配置Nginx靜態(tài)服務(wù)器

下載nginx,解壓之后,打開conf文件夾下面的nginx.conf

設(shè)置靜態(tài)文件路徑,然后在根目錄執(zhí)行nginx啟動(dòng),靜態(tài)文件服務(wù)器就可以使用了

server {
        listen       8088;
        server_name  127.0.0.1;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            alias   D:/work/file/;
            index  index.html index.htm;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

文件夾下放了一張圖片,我們打開瀏覽器,輸入http://127.0.0.1/cat.jpeg就可以訪問到了

2.編寫后臺(tái)授權(quán)接口

正常我們都是通過hearder中攜帶授權(quán)token信息,所以我們后臺(tái)寫個(gè)接口,通過HttpServletRequest獲取header中的token信息,再進(jìn)行業(yè)務(wù)的驗(yàn)證就可以了,auth_request模塊是根據(jù)返回的http狀態(tài)值來判斷是否通過授權(quán),200則為成功,401或者403為授權(quán)失敗

    @RequestMapping("/authFileValid")
    @ResponseBody
    public void authFileValid(HttpServletRequest request,HttpServletResponse response){
        String token = request.getHeader("accessToken");
        System.out.println("獲取的token:"+token);
        if(token != null){
            //驗(yàn)證token是否合法
        }else{
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
        }
    }

3.修改nginx配置文件

server {
        listen       8088;
        server_name  127.0.0.1;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            alias   D:/work/file/;
            # 設(shè)置鑒權(quán)的請求
            auth_request /authFileValid;
            # 從查詢參數(shù)中獲取 token,并賦值給token變量
            set $token $arg_token;
            # 自定義驗(yàn)證失敗時(shí)的處理頁面
            error_page 401 = /auth-required; 
        }
 
        location = /authFileValid {
            internal; # 只允許內(nèi)部訪問
            proxy_pass http://127.0.0.1:8080/authFileValid;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_set_header X-Original-URI $request_uri;
            # 設(shè)置AccessToken 的值為token
            proxy_set_header AccessToken "$token";
        }
 
        location = /auth-required {
             return 401; # 返回 401 狀態(tài)碼
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

注意:

set $token $arg_token中的arg_是參數(shù)前綴固定寫法,實(shí)則是獲取的查詢參數(shù)中的token值

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

4.測試

重新啟動(dòng)nginx,啟動(dòng)后臺(tái)web,瀏覽器訪問http://127.0.0.1:8088/cat.jpeg,就可以看到下面的結(jié)果了

  • 前端:

  • 后臺(tái):

我們可以看到,再次訪問圖片返回了401,這時(shí)候我們已經(jīng)沒有權(quán)限去訪問圖片了

這次我們隨便設(shè)置一下token值,后臺(tái)并沒有進(jìn)行驗(yàn)證token的正確性,便于測試只是驗(yàn)證了非空

  • 前端:

  • 后端:

如此我們便實(shí)現(xiàn)了nginx調(diào)用后臺(tái)接口授權(quán)的整個(gè)流程、

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論