您好,欢迎访问一九零五行业门户网

nginx+lua实现登陆验证

用于在多台服务器上单点登录sso、无session,用户身份的 验证 。 1、安装lua yum install readline.x86_64 readline-devel.x86_64 wget http://www.lua.org/ftp/lua-5.1.5.tar.gz make linux make install 注意:不要使用5.2版本,5.2版本的lua和nginx的整合
用于在多台服务器上单点登录sso、无session,用户身份的验证。
1、安装lua yum install readline.x86_64 readline-devel.x86_64
wget http://www.lua.org/ftp/lua-5.1.5.tar.gz make linux make install 注意:不要使用5.2版本,5.2版本的lua和nginx的整合有问题,编译会报错:
lua_globalsindex' undeclared (first use in this function) 参考:https://github.com/lualanes/lanes/issues/18
2、编译nginx
下载lua-nginx-module
wget https://github.com/chaoslawful/lua-nginx-module/zipball/master file master unzip master mv chaoslawful-lua-nginx-module-06d654b/ lua-nginx-module 下载ngx_devel_kit
https://github.com/simpl/ngx_devel_kit/zipball/master file master unzip master mv simpl-ngx_devel_kit-4192ba6/ simpl-ngx_devel_kit 编译nginx
tar -xvzf nginx-1.2.1.tar.gz ./configure \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --without-poll_module \ --without-select_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_perl_module \ --add-module=../simpl-ngx_devel_kit \ --add-module=../lua-nginx-module make  make install
2、测试lua
测试
location = /lua { content_by_lua ' ngx.say(hello, lua!) '; }
3、登录验证
nginx添加配置
access_by_lua_file 'conf/access.lua'; access.lua: 可以根据需要添加更多的验证域
local secretkey='1234567890abcdefghi' if ngx.var.cookie_uid == nil or ngx.var.cookie_token == nil then ngx.req.set_header(check-login, null) return end
--local ctoken = ngx.md5('uid:' .. ngx.var.cookie_uid .. '&secretkey:' .. secretkey) local ctoken = ngx.md5(ngx.var.cookie_uid .. secretkey) if ctoken == ngx.var.cookie_token then ngx.req.set_header(check-login, yes) else ngx.req.set_header(check-login, no) end return 如果uid+lua中的securekey的md5值和请求中的cookie的值一致,则设置request header中的http_check_login为yes,否则为no,如果不存在uid或token这两个cookie中的一个,则http_check_login设置为null。
关于check-login,http_check_login:
lua中设置的heaer为check-login,输出后就变成了http_check_login,即前面加了http_,经过测试,有些会添加http_,而有些则不会添加,如content-type。
详细信息查看:http://wiki.nginx.org/httpluamodule#ngx.req.set_header
4、测试
使用perl cgi
perl打印env
#!/usr/bin/perl -w
use strict;
use cgi;
use data::dumper;
my $query = new cgi;
print $query->header('text/html');
print dumper \%env;
#if ($env{http_check_login} ne yes){
# print not auth;
# exit;
#} 打印\%env哈希可以看到我们添加的header。
注释部分是一个例子,针对认证的结果做更多的操作。
可以使用curl来带着cookie进行测试:
curl -b uid=1234;token=8323d8c4a0533dc78c7051a074cdb286 http://127.0.0.1/7.cgi
如果使用echo 打印md5值,需要使用-n参数去掉回车
echo -n 123456789|md5sum
如何查看lua生成的md5?
location = /lua {
            content_by_lua '
                 local ctoken = ngx.md5(12345 .. 6789)
                ngx.say(ctoken)       
            ';
        }
关于查看access.lua生成的cookie
local secretkey='cookiesecretkey' if ngx.var.cookie_uid == nil or ngx.var.cookie_token == nil then ngx.req.set_header(check-login, null) return end --local ctoken = ngx.md5('uid:' .. ngx.var.cookie_uid .. '&secretkey:' .. secretkey) local ctoken = ngx.md5(ngx.var.cookie_uid .. secretkey) if ctoken == ngx.var.cookie_token then ngx.req.set_header(check-login, yes) print (ctoken) print (ngx.var.cookie_token) else ngx.req.set_header(check-login, no) print (ctoken) print (ngx.var.cookie_token) end return 打开nginx的log到debug,从error里可以看到access.lua的输出
通过html种植cookie
html xmlns=http://www.w3.org/1999/xhtml>
head>
meta http-equiv=content-type content=text/html; charset=gbk>
/head>
body>
p>
script>
document.cookie=domain=intercom.com.cn;
document.cookie=uid=1234;
document.cookie=token=dbd19902c04fdc68ee8b97510f454614;
//document.cookie=expires=sat, 31-dec-39 23:59:59 gmt;
document.write(document.cookie);
/script>
/p>
/body>
/html>
其它类似信息

推荐信息