tomcat7
 nginx 1.4.6(更新后1.5.6)

項目中經常會用到權限管理,必然的就會存在權限的設定和驗證;對于登陸或者模塊的權限設定驗證,在項目中直接實現;那么問題出現了

1.訪問資源文件

2.多項目訪問權限

3.tomcat中虛擬目錄的訪問權限

公司項目中用到文件的上傳下載,在線預覽等功能;當然用戶在使用的時候,我們不可能把用戶的上傳的文件放在項目中,那么必然會用到

虛擬目錄來映射文件的位置,或者說跨域夸項目;如果沒有對這些進行一個訪問的權限的限制,那么你將可以不需要任何權限就可以訪問下載

服務器上的資源(只要路徑正確不需要任何權限),下面拿公司項目實際情況說明一下:

比如訪問: http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/上傳應用描述.docx 將出現

filefindPDFfilePath?filePath=root/(A)/上傳應用描述.docx http 請求會返回 /imgss/(A)/上傳應用描述.docx(imgss是tomcat server.xml配置的虛擬目錄)

打開瀏覽器我們直接訪問虛擬目錄:http://192.168.1.118/imgss/(A)/上傳應用描述.docx 將出現不需要任何權限可以預覽或者使用工具下載服務器資源

這會導致客戶在使用時,在稍有心思的人面前沒有任何安全可言

下面說一下如何解決這個問題:

第一種方案:設置tomcat全局過濾器,進行session驗證

1.項目中添加過濾器 UserLoginFilter.java(這里就不贅述了)

2.在tomcat 配置文件web.xml添加Filter

注意這里:applicaton/com.rhxy.service.UserLoginFilter 指向項目中的UserLoginFilter過濾器

這里需要說明一下。配置的攔截請求都是tomcat server.xml中配置的虛擬路徑

而所有的訪問資源的請求都是需要經過這里,所以直接設置虛擬路徑即可

啟動tomcat 重新部署項目;進行測試。結果如下

未登錄情況下

訪問:http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png 打開tomcat 日志進行觀察:

觀察一下前臺:

未登錄情況下被成功被過濾掉了

測試直接訪問虛擬目錄:http://192.168.1.118/imgss/(A)/12.png同樣被攔截 

然后測試登陸后訪問,就出現問題了。請求是被過濾了,但是權限驗證合格以后請求返回的status 居然是404 

后臺日志:

前臺:

http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png

http://192.168.1.118/imgss/(A)/12.png  通過后臺觀察,請求都是有經過過濾器進行session驗證,并且都是驗證通過,然而還是出現404,資源在

服務器中是肯定存在的,tomcat 中的虛擬目錄設置也沒有問題;經過多次測試問題出在tomcat配置文件web.xml配置的過濾器,映射的是項目中的過濾器

那么將過濾器打包成jar 以后,放置在tomcat 中,然后修改web.xml配置文件,修改Filter 中filter-class 為jar包打包的路徑,然后測試發現項目無法啟動。

在配置Filter 中filter-class 路徑時出錯;然后經過一系列嘗試,最終還是失敗,因為時間有限,所以只能放棄這種,但是我相信思路肯定是正確的,因為

聽說已經有人實現了,但是沒有具體說明,下面開始嘗試第二種

第二種方案:通過nginx代理攔截請求,并進行訪問限制

1.使用 nginx -V 查看版本幾安裝的模塊,查看是否有ngx_http_accesskey_module模塊,沒有的話需要為nginx加入ngx_http_accesskey_module模塊(附件),因為我的版本是1.4.6上面很多模塊都沒有,先進行升級

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)進入ngixn-1.5.6文件夾中

cd nginx-1.5.6

1.4)查看nginx原來的配置輸出:

–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)執行configure命令,后面跟上原來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

在執行configure時得到幾個錯誤:

a.配置 –with-http_xslt_module 時提示 the HTTP XSLT module requires the libxml2/libxslt libraries

sudo apt-get install libxml2 libxml2-dev libxslt-dev 

b.配置 –with-http_image_filter_module 時提示 the HTTP image filter module requires the GD library.

sudo apt-get install libgd2-xpm libgd2-xpm-dev

c.配置 –with-http_geoip_module 時提示 the GeoIP module requires the GeoIP library.

sudo apt-get install geoip-database libgeoip-dev

d.rewrite需要pcre支持, 錯誤提示:./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)再執行第6步的configure命令

3.3)這次沒有提示缺少library, 執行make令命編譯nginx, 編譯好以后objs目錄下多出一個nginx文件,這個就是已編輯好的nginx程序

make

3.4)更改舊的nginx程序的名子,并復制新的程序過去,我的舊nginx程序放在/usr/sbin/目錄中

mv /usr/sbin/nginx /usr/sbin/nginx-20160910
cp objs/nginx /usr/sbin/nginx
/usr/sbin/nginx -t

執行/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目錄下執行下面的命令來升級nginx

make upgrade

3.6)執行make upgrade得到一個錯誤:

make: /etc/nginx/sbin/nginx: Command not foundmake: *** [upgrade] Error 127

3.7)用文本編輯器打開修改nginx-1.5.6目錄下名為Makefile的文件,將upgrade節點中的/etc/nginx/sbin/nginx -t改為/usr/sbin/nginx -t,保存后關閉并重新執行make upgrade命令

3.8)執行nginx -V命令,程序的版本號已經是1.5.6,升級完畢.

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 為模塊開關;
accesskey_hashmethod 為加密方式MD5或者SHA-1;
accesskey_arg 為url中的關鍵字參數;
accesskey_signature 為xxxxxxx加密值構成的字符串。

保存后,重啟nginx :service nginx restart  ?sign=(這里為accesskey_signature MD5加密值)

再次進行測試,使用http://192.168.1.118/filefindPDFfilePath?filePath=root/(A)/12.png進行訪問,將請求返回的URl /imgss/(A)/上傳應用描述.docx后面添加?sign=(這里為accesskey_signature MD5加密值)

測試結果:未登錄情況下 http://192.168.1.118/filefindPDFfilePath?

filePath=root/(A)/12.png http://192.168.1.118/imgss/(A)/12.png

都將返回404頁面。登陸后返回正常頁面,顯示12.png

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持三五互聯。

更多關于云服務器,域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

贊(0)
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:三五互聯知識庫 » 通過nginx代理攔截請求進行全局訪問限制

登錄

找回密碼

注冊

主站蜘蛛池模板: 男女无遮挡激情视频| 99精品久久毛片a片| 国产自在自线午夜精品| 成人免费无遮挡无码黄漫视频| 亚洲欧美人成电影在线观看| 男女啪啪免费观看网站| 精品乱码一区二区三四五区 | 日韩精品一区二区三区在线观看| 2020国产欧洲精品网站| 亚洲欧美高清在线精品一区二区| 国产一区二区不卡91| 亚洲天堂网色图伦理经典| 亚洲精品不卡无码福利在线观看| 亚洲鸥美日韩精品久久| 蜜臀av久久国产午夜| 久久亚洲精品成人综合网| 国产短视频一区二区三区| 少妇人妻偷人免费观看| 国产精品流白浆无遮挡| 海丰县| 国产成人午夜福利在线观看| 亚洲高清WWW色好看美女| 欧美色综合天天久久综合精品| 成人一区二区不卡国产| 亚洲最大有声小说AV网| 成年女人片免费视频播放A| 国产在线精品欧美日韩电影| 美日韩精品综合一区二区| 日韩一区二区三区高清视频| 亚洲天堂男人的天堂在线| 久久久久久久一线毛片| 在厨房拨开内裤进入在线视频| av天堂久久精品影音先锋 | 亚洲av优女天堂熟女久久| 久久91精品牛牛| 新乐市| 久久久久久久久毛片精品| 久久热这里这里只有精品| 国产成人免费午夜在线观看| 亚洲精品综合第一国产综合| 国产成人久久精品二区三|