本文介紹了通過(guò)nginx代理攔截請(qǐng)求進(jìn)行全局訪問(wèn)限制,分享給大家,具體如下:
運(yùn)行環(huán)境:
- Ubantu 14.0
- tomcat7
- nginx 1.4.6(更新后1.5.6)
項(xiàng)目中經(jīng)常會(huì)用到權(quán)限管理,必然的就會(huì)存在權(quán)限的設(shè)定和驗(yàn)證;對(duì)于登陸或者模塊的權(quán)限設(shè)定驗(yàn)證,在項(xiàng)目中直接實(shí)現(xiàn);那么問(wèn)題出現(xiàn)了
1.訪問(wèn)資源文件
2.多項(xiàng)目訪問(wèn)權(quán)限
3.tomcat中虛擬目錄的訪問(wèn)權(quán)限
公司項(xiàng)目中用到文件的上傳下載,在線預(yù)覽等功能;當(dāng)然用戶在使用的時(shí)候,我們不可能把用戶的上傳的文件放在項(xiàng)目中,那么必然會(huì)用到
虛擬目錄來(lái)映射文件的位置,或者說(shuō)跨域夸項(xiàng)目;如果沒(méi)有對(duì)這些進(jìn)行一個(gè)訪問(wèn)的權(quán)限的限制,那么你將可以不需要任何權(quán)限就可以訪問(wèn)下載
服務(wù)器上的資源(只要路徑正確不需要任何權(quán)限),下面拿公司項(xiàng)目實(shí)際情況說(shuō)明一下:
比如訪問(wèn): http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/上傳應(yīng)用描述.docx 將出現(xiàn)
![](/d/20211016/fb594db0c95bb421e504c927ba85bf19.gif)
filefindPDFfilePath?filePath=root/(A)/上傳應(yīng)用描述.docx http 請(qǐng)求會(huì)返回 /imgss/(A)/上傳應(yīng)用描述.docx(imgss是tomcat server.xml配置的虛擬目錄)
打開(kāi)瀏覽器我們直接訪問(wèn)虛擬目錄:http://192.168.1.118/imgss/(A)/上傳應(yīng)用描述.docx 將出現(xiàn)不需要任何權(quán)限可以預(yù)覽或者使用工具下載服務(wù)器資源
![](/d/20211016/904c760cc0435689e816582052c3cdd4.gif)
這會(huì)導(dǎo)致客戶在使用時(shí),在稍有心思的人面前沒(méi)有任何安全可言
下面說(shuō)一下如何解決這個(gè)問(wèn)題:
第一種方案:設(shè)置tomcat全局過(guò)濾器,進(jìn)行session驗(yàn)證
1.項(xiàng)目中添加過(guò)濾器 UserLoginFilter.java(這里就不贅述了)
2.在tomcat 配置文件web.xml添加Filter
![](/d/20211016/43006eb5b6507691be07ca483533863c.gif)
注意這里:applicaton/com.rhxy.service.UserLoginFilter 指向項(xiàng)目中的UserLoginFilter過(guò)濾器
這里需要說(shuō)明一下。配置的攔截請(qǐng)求都是tomcat server.xml中配置的虛擬路徑
而所有的訪問(wèn)資源的請(qǐng)求都是需要經(jīng)過(guò)這里,所以直接設(shè)置虛擬路徑即可
啟動(dòng)tomcat 重新部署項(xiàng)目;進(jìn)行測(cè)試。結(jié)果如下
未登錄情況下
訪問(wèn):http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png 打開(kāi)tomcat 日志進(jìn)行觀察:
![](/d/20211016/f26f53e006e2f4d809034dbe57179974.gif)
觀察一下前臺(tái):
![](/d/20211016/f33b7906f45897b8d82973b5fbf490a0.gif)
![](/d/20211016/ce52f14142d850e0a16ac2cefe603647.gif)
未登錄情況下被成功被過(guò)濾掉了
測(cè)試直接訪問(wèn)虛擬目錄:http://192.168.1.118/imgss/(A)/12.png同樣被攔截
然后測(cè)試登陸后訪問(wèn),就出現(xiàn)問(wèn)題了。請(qǐng)求是被過(guò)濾了,但是權(quán)限驗(yàn)證合格以后請(qǐng)求返回的status 居然是404
后臺(tái)日志:
![](/d/20211016/79a3e82dcb1ec6a09da08169018a8730.gif)
前臺(tái):
![](/d/20211016/4a8d999eac49a81e388eb8dcdcbff890.gif)
![](/d/20211016/1ef4b377cb868e6b88c843f548bfca06.gif)
http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png
http://192.168.1.118/imgss/(A)/12.png 通過(guò)后臺(tái)觀察,請(qǐng)求都是有經(jīng)過(guò)過(guò)濾器進(jìn)行session驗(yàn)證,并且都是驗(yàn)證通過(guò),然而還是出現(xiàn)404,資源在
服務(wù)器中是肯定存在的,tomcat 中的虛擬目錄設(shè)置也沒(méi)有問(wèn)題;經(jīng)過(guò)多次測(cè)試問(wèn)題出在tomcat配置文件web.xml配置的過(guò)濾器,映射的是項(xiàng)目中的過(guò)濾器
那么將過(guò)濾器打包成jar 以后,放置在tomcat 中,然后修改web.xml配置文件,修改Filter 中filter-class 為jar包打包的路徑,然后測(cè)試發(fā)現(xiàn)項(xiàng)目無(wú)法啟動(dòng)。
在配置Filter 中filter-class 路徑時(shí)出錯(cuò);然后經(jīng)過(guò)一系列嘗試,最終還是失敗,因?yàn)闀r(shí)間有限,所以只能放棄這種,但是我相信思路肯定是正確的,因?yàn)?/p>
聽(tīng)說(shuō)已經(jīng)有人實(shí)現(xiàn)了,但是沒(méi)有具體說(shuō)明,下面開(kāi)始嘗試第二種
第二種方案:通過(guò)nginx代理攔截請(qǐng)求,并進(jìn)行訪問(wèn)限制
1.使用 nginx -V 查看版本幾安裝的模塊,查看是否有ngx_http_accesskey_module模塊,沒(méi)有的話需要為nginx加入ngx_http_accesskey_module模塊(附件),因?yàn)槲业陌姹臼?.4.6上面很多模塊都沒(méi)有,先進(jìn)行升級(jí)
1.1)下載nginx-1.5.6.tar.gz (文件位置在/root下)
wget http://nginx.org/download/nginx-1.5.6.tar.gz
1.2)解壓nginx-1.5.6.tar.gz文件
tar zxvf nginx-1.5.6.tar.gz
1.3)進(jìn)入ngixn-1.5.6文件夾中
1.4)查看nginx原來(lái)的配置輸出:
--prefix=/etc/nginx--conf-path=/etc/ nginx/nginx.conf--error-log-path=/var/log/nginx/error.log --http-client-body-te mp-path=/var/lib/nginx/body--http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log--http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi--http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid--with-http_realip_module --with-http_stub_status_module--with-http_ssl_module
--with-debug--with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6--with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl
--with-mail--with-mail_ssl_module
2.下載ngx_http_accesskey_module模塊
2.1)mkdir -p /etc/nginx/third-modules
2.2)cd /etc/nginx/third-modules
2.3)wget ftp://91.193.69.2/distributive/FreeBSD/ports/local-distfiles/osa/nginx-accesskey-2.0.3.tar.gz
2.4)tar -zxvf nginx-accesskey-2.0.3.tar.gz
2.5)修改下nginx-accesskey-2.0.3的config文件:把$HTTP_ACCESSKEY_MODULE修改成ngx_http_accesskey_module
3.返回root下nginx-1.5.6
3.1)執(zhí)行configure命令,后面跟上原來(lái)nginx的配置 并添加ngx_http_accesskey_module
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/etc/nginx/third-modules/nginx-accesskey-2.0.3
在執(zhí)行configure時(shí)得到幾個(gè)錯(cuò)誤:
a.配置 --with-http_xslt_module 時(shí)提示 the HTTP XSLT module requires the libxml2/libxslt libraries
sudo apt-get install libxml2 libxml2-dev libxslt-dev
b.配置 --with-http_image_filter_module 時(shí)提示 the HTTP image filter module requires the GD library.
sudo apt-get install libgd2-xpm libgd2-xpm-dev
c.配置 --with-http_geoip_module 時(shí)提示 the GeoIP module requires the GeoIP library.
sudo apt-get install geoip-database libgeoip-dev
d.rewrite需要pcre支持, 錯(cuò)誤提示:./configure: error: the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev
e.ubuntu error: SSL modules require the OpenSSL library.
sudo apt-get install openssl libssl-dev
3.2)再執(zhí)行第6步的configure命令
3.3)這次沒(méi)有提示缺少library, 執(zhí)行make令命編譯nginx, 編譯好以后objs目錄下多出一個(gè)nginx文件,這個(gè)就是已編輯好的nginx程序
3.4)更改舊的nginx程序的名子,并復(fù)制新的程序過(guò)去,我的舊nginx程序放在/usr/sbin/目錄中
mv /usr/sbin/nginx /usr/sbin/nginx-20160910
cp objs/nginx /usr/sbin/nginx
/usr/sbin/nginx -t
執(zhí)行/usr/sbin/nginx -t 命令檢查配置文件并將返回下面的信息:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3.5)在nginx-1.5.6目錄下執(zhí)行下面的命令來(lái)升級(jí)nginx
3.6)執(zhí)行make upgrade得到一個(gè)錯(cuò)誤:
make: /etc/nginx/sbin/nginx: Command not foundmake: *** [upgrade] Error 127
3.7)用文本編輯器打開(kāi)修改nginx-1.5.6目錄下名為Makefile的文件,將upgrade節(jié)點(diǎn)中的/etc/nginx/sbin/nginx -t改為/usr/sbin/nginx -t,保存后關(guān)閉并重新執(zhí)行make upgrade命令
3.8)執(zhí)行nginx -V命令,程序的版本號(hào)已經(jīng)是1.5.6,升級(jí)完畢.
4.修改nginx配置文件 vim /etc/nginx/sites-available/localhost
location ^~ /imgss/
{
# root /data/customfiles/photos/;
# expires 30d;
accesskey off;
accesskey_arg "sign";
accesskey_hashmethod md5;
accesskey_signature "xxxxxxx";
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
accesskey 為模塊開(kāi)關(guān);
accesskey_hashmethod 為加密方式MD5或者SHA-1;
accesskey_arg 為url中的關(guān)鍵字參數(shù);
accesskey_signature 為xxxxxxx加密值構(gòu)成的字符串。
保存后,重啟nginx :service nginx restart ?sign=(這里為accesskey_signature MD5加密值)
再次進(jìn)行測(cè)試,使用http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png進(jìn)行訪問(wèn),將請(qǐng)求返回的URl /imgss/(A)/上傳應(yīng)用描述.docx后面添加?sign=(這里為accesskey_signature MD5加密值)
測(cè)試結(jié)果:未登錄情況下 http://192.168.1.118/filefindPDFfilePath?
filePath=root/(A)/12.png http://192.168.1.118/imgss/(A)/12.png
都將返回404頁(yè)面。登陸后返回正常頁(yè)面,顯示12.png
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。