使用Nginx做靜態(tài)文件服務(wù)器,如何進(jìn)行權(quán)限驗(yàn)證
前言
在我們的日常開發(fā)過程中,經(jīng)常使用nginx做文件讀取服務(wù)器,因?yàn)榕渲梅浅:?jiǎn)單,方便使用。只要通過IP和端口加上文件路徑就可以讀到文件或者圖片了。但是,我們的安全問題該如何處理?并不是所有的人拿到圖片路徑就可以訪問文件,這樣很有可能造成文件泄露。
因此,我們想的是,在通過路徑獲取文件的時(shí)候,可以攜帶token信息,通過我們的系統(tǒng)服務(wù)進(jìn)行token驗(yàn)證,如果token合法,才能成功獲取圖片,否則拒絕此次請(qǐng)求。
以下是具體的實(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)的請(qǐng)求
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.測(cè)試
重新啟動(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的正確性,便于測(cè)試只是驗(yàn)證了非空
- 前端:

- 后端:

如此我們便實(shí)現(xiàn)了nginx調(diào)用后臺(tái)接口授權(quán)的整個(gè)流程、
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx配置靜態(tài)資源的訪問的實(shí)現(xiàn)
本文主要介紹了nginx配置靜態(tài)資源的訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
Nginx出現(xiàn)403錯(cuò)誤,應(yīng)該如何解決
這篇文章主要介紹了Nginx出現(xiàn)403錯(cuò)誤,應(yīng)該如何解決?具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Nginx報(bào)錯(cuò)"Too many open files"問題的深度解析
在高并發(fā)場(chǎng)景下,Nginx作為Web服務(wù)器或反向代理時(shí),常常會(huì)遇到“Too many open files”錯(cuò)誤,本文將從問題原理,解決方案,配置優(yōu)化及驗(yàn)證方法等方面,詳細(xì)解析如何解決這一問題,希望對(duì)大家有所幫助2025-06-06
解決Nginx 配置 proxy_pass 后 返回404問題
這篇文章主要介紹了Nginx 配置 proxy_pass 后 返回404問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
詳解nginx過濾url實(shí)現(xiàn)前臺(tái)js的配置問題
本篇文章主要介紹了nginx過濾url實(shí)現(xiàn)前臺(tái)js的配置問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
詳解Nginx 出現(xiàn) 403 Forbidden 的解決辦法
本篇文章主要介紹了詳解Nginx 出現(xiàn) 403 Forbidden 的解決辦法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08

