0
点赞
收藏
分享

微信扫一扫

Chrome 插件各模块之间的消息传递

宁静的猫 03-30 15:00 阅读 2

需求背景:比如我们有一个存储文件的web服务器,一般通过url可直接访问到:http://127.0.0.1/uploads/test.rar,如果我们需要限制别人的访问,可以通过添加lua脚本来控制url访问权限,以下是实现步骤。

安装LuaJIT

下载地址:http://luajit.org/download.html

tar zxf LuaJIT-2.1.0-beta2.tar.gz
cd LuaJIT-2.1.0-beta2
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

下载ngx_devel_kit模块

下载地址:https://github.com/simpl/ngx_devel_kit/tags
目前最新版本:https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz

tar -xzvf v0.3.0.tar.gz # 不需要安装

下载lua-nginx-module模块

下载地址:https://github.com/openresty/lua-nginx-module/tags
目前最新版本:https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz

tar -xzvf v0.10.7.tar.gz # 不需要安装

重新编译安装Nginx

nginx -V查看已经编译的配置

nginx -V

输出结果:

--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

进入Nginx源码目录,重新编译,加上以下参数(注意以下module解压路径)

--with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/path/to/ngx_devel_kit-0.3.0 --add-module=/path/to/lua-nginx-module-0.10.7

完整编译配置如下:

# 设置环境变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.1
# 配置
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt=-Wl,-rpath,/usr/local/luajit/lib --add-module=/home/vagrant/ngx_devel_kit-0.3.0 --add-module=/home/vagrant/lua-nginx-module-0.10.7
# 编译安装
make -j2
make install

配置Nginx.conf

在/usr/local/nginx/conf/nginx.conf中的server节加入如下代码:

location ^~ /uploads/ {
            access_by_lua '
                local secret_key = "prefix_eu56#42dfd6g*@"
                local diff_time  = 10
                local local_time = ngx.time()
                local timestamp = tonumber(ngx.var.arg_timestamp)
                local token     = ngx.var.arg_token
 
                if (timestamp == nil or token == nil) then
                    ngx.exit(403)
                elseif (timestamp + diff_time < local_time) then
                    ngx.exit(403)
                end
 
                local access_token = ngx.md5(timestamp..secret_key)
 
                if token == access_token then
                    return true
                else
                    ngx.exit(403)
                end
            ';
        }

重启Nginx

service nginx restart

检查生效状态

访问URL:http://127.0.0.1/uploads/test.rar?token=52f0b2b4ebedb0094eff53383098a4b8&timestamp=1487901531

权限判断规则:

1、URL必须包含timestamp和token参数
2、timestamp为秒级时间戳,10s之内时间有效
3、token为md5(timestamp+secret_key)

时间过期输出403 Forbidden
验证成功返回文件。

举报

相关推荐

0 条评论