通过nginx代理拦截请求进行全局访问限制
通过nginx代理拦截请求进⾏全局访问限制
本⽂介绍了通过nginx代理拦截请求进⾏全局访问限制,分享给⼤家,具体如下:
运⾏环境:
1. Ubantu 14.0
2. tomcat7
3.  nginx 1.
4.6(更新后1.
5.6)
项⽬中经常会⽤到权限管理,必然的就会存在权限的设定和验证;对于登陆或者模块的权限设定验证,在项⽬中直接实现;那么问题出现了
1.访问资源⽂件
2.多项⽬访问权限
公司项⽬中⽤到⽂件的上传下载,在线预览等功能;当然⽤户在使⽤的时候,我们不可能把⽤户的上传的⽂件放在项⽬中,那么必然会⽤到
虚拟⽬录来映射⽂件的位置,或者说跨域夸项⽬;如果没有对这些进⾏⼀个访问的权限的限制,那么你将可以不需要任何权限就可以访问下载
服务器上的资源(只要路径正确不需要任何权限),下⾯拿公司项⽬实际情况说明⼀下:
filefindPDFfilePath?filePath=root/(A)/上传应⽤描述.docx http 请求会返回 /imgss/(A)/上传应⽤描述.docx(imgss是l配置的虚拟⽬录)
这会导致客户在使⽤时,在稍有⼼思的⼈⾯前没有任何安全可⾔
下⾯说⼀下如何解决这个问题:
第⼀种⽅案:设置tomcat全局过滤器,进⾏session验证
1.项⽬中添加过滤器 UserLoginFilter.java(这⾥就不赘述了)
2.在tomcat 配置⽂件l添加Filter
注意这⾥:applicaton/com.rhxy.service.UserLoginFilter 指向项⽬中的UserLoginFilter过滤器
这⾥需要说明⼀下。配置的拦截请求都是l中配置的虚拟路径
⽽所有的访问资源的请求都是需要经过这⾥,所以直接设置虚拟路径即可
启动tomcat 重新部署项⽬;进⾏测试。结果如下
未登录情况下
观察⼀下前台:
未登录情况下被成功被过滤掉了
然后测试登陆后访问,就出现问题了。请求是被过滤了,但是权限验证合格以后请求返回的status 居然是404
后台⽇志:
前台:继承者们结局是什么
服务器中是肯定存在的,tomcat 中的虚拟⽬录设置也没有问题;经过多次测试问题出在tomcat配置⽂件l配置的过滤器,映射的是项⽬中的过滤器
那么将过滤器打包成jar 以后,放置在tomcat 中,然后修改l配置⽂件,修改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. (⽂件位置在/root下)
wget /download/nginx-1.5.
1.2)解压nginx-1.5.⽂件
tar zxvf nginx-1.5.
1.3)进⼊ngixn-1.5.6⽂件夹中
cd nginx-1.5.6
1.4)查看nginx原来的配置输出:
世界三大宗教--prefix=/etc/nginx--conf-path=/etc/ f--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.19
3.69.2/distributive/FreeBSD/ports/local-distfiles/osa/nginx-accesskey-2.0.
2.4)tar -zxvf nginx-accesskey-2.0.
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/f --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
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/f syntax is ok
nginx: configuration file /etc/f 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
安徽高考时间2020具体时间
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 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加密值)
都将返回404页⾯。登陆后返回正常页⾯,显⽰12.png钉钉怎么签到
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。