今天小编给大家分享一下通过Nginx代理拦截请求进行全局访问限制的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
运行环境:
ubantu 14.0
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(这里就不赘述了)
注意这里: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 路径时出错;然后经过一系列尝试,最终还是失败,因为时间有限,所以只能放弃这种,但是我相信思路肯定是正确的,因为
听说已经有人实现了,但是没有具体说明,下面开始尝试第二种
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
tar zxvf Nginx-1.5.6.tar.gz
1.3)进入ngixn-1.5.6文件夹中
cd Nginx-1.5.6
--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
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
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
以上就是“通过Nginx代理拦截请求进行全局访问限制的方法”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程之家行业资讯频道。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。