nginx+lua实现登陆验证

用于在多台服务器上单点登录SSO、无SESSION,用户身份的验证。
1、安装lua
yum install readline.x86_64 readline-devel.x86_64
  1. wget http://www.lua.org/ftp/lua-5.1.5.tar.gz
  2. make linux
  3. make install
注意:不要使用5.2版本,5.2版本的lua和nginx的整合有问题,编译会报错:
  1. LUA_GLOBALSINDEX' undeclared (first use in this function)

2、编译nginx
下载lua-nginx-module
  1. wget https://github.com/chaoslawful/lua-nginx-module/zipball/master
  2. file master
  3. unzip master
  4. mv chaoslawful-lua-nginx-module-06d654b/ lua-nginx-module
下载ngx_devel_kit
  1. https://github.com/simpl/ngx_devel_kit/zipball/master
  2. file master
  3. unzip master
  4. mv simpl-ngx_devel_kit-4192ba6/ simpl-ngx_devel_kit
编译nginx
  1. tar -xvzf nginx-1.2.1.tar.gz
  2. ./configure \
  3. --prefix=/usr/local/nginx \
  4. --with-http_stub_status_module \
  5. --without-poll_module \
  6. --without-select_module \
  7. --with-http_ssl_module \
  8. --with-http_realip_module \
  9. --with-http_perl_module \
  10. --add-module=../simpl-ngx_devel_kit \
  11. --add-module=../lua-nginx-module
make 
make install

2、测试lua
测试

  1. location = /lua {
  2. content_by_lua '
  3. ngx.say("Hello, Lua!")
  4. ';
  5. }

3、登录验证
nginx添加配置

  1. access_by_lua_file 'conf/access.lua';
access.lua:
可以根据需要添加更多的验证域
  1. local secretkey='1234567890abcdefghi'
  2. if ngx.var.cookie_uid == nil or ngx.var.cookie_token == nil then
  3. ngx.req.set_header("Check-Login", "NULL")
  4. return
  5. end

  6. --local ctoken = ngx.md5('uid:' .. ngx.var.cookie_uid .. '&secretkey:' .. secretkey)
  7. local ctoken = ngx.md5(ngx.var.cookie_uid .. secretkey)
  8. if ctoken == ngx.var.cookie_token then
  9. ngx.req.set_header("Check-Login", "YES")
  10. else
  11. ngx.req.set_header("Check-Login", "NO")
  12. end
  13. 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。


4、测试
使用perl cgi
perl打印ENV

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use CGI;
  4. use Data::Dumper;

  5. my $query = new CGI;
  6. print $query->header('text/html');

  7. print Dumper \%ENV;

  8. #if ($ENV{HTTP_CHECK_LOGIN} ne "YES"){
  9. # print "not auth";
  10. # exit;
  11. #}
打印\%ENV哈希可以看到我们添加的header。

注释部分是一个例子,针对认证的结果做更多的操作。

可以使用curl来带着cookie进行测试:
curl -b "uid=1234;token=8323d8c4a0533dc78c7051a0 74cdb286" 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

  1. local secretkey='cookiesecretKey'
  2. if ngx.var.cookie_uid == nil or ngx.var.cookie_token == nil then
  3. ngx.req.set_header("Check-Login", "NULL")
  4. return
  5. end
  6. --local ctoken = ngx.md5('uid:' .. ngx.var.cookie_uid .. '&secretkey:' .. secretkey)
  7. local ctoken = ngx.md5(ngx.var.cookie_uid .. secretkey)
  8. if ctoken == ngx.var.cookie_token then
  9. ngx.req.set_header("Check-Login", "YES")
  10. print (ctoken)
  11. print (ngx.var.cookie_token)
  12. else
  13. ngx.req.set_header("Check-Login", "NO")
  14. print (ctoken)
  15. print (ngx.var.cookie_token)
  16. end
  17. return
打开nginx的log到debug,从error里可以看到access.lua的输出


通过html种植cookie
  1. <</span>html xmlns="http://www.w3.org/1999/xhtml">
  2. <</span>head>
  3. <</span>meta http-equiv="Content-Type" content="text/html; charset=gbk">
  4. <</span>/head>

  5. <</span>body>
  6. <</span>p>
  7. <</span>script>
  8. document.cookie="domain=intercom.com.cn"; 
  9. document.cookie="uid=1234"; 
  10. document.cookie="token=dbd19902c04fdc68ee8b97510f454614"; 
  11. //document.cookie="expires=Sat, 31-Dec-39 23:59:59 GMT"; 
  12. document.write(document.cookie); 
  13. <</span>/script>
  14. <</span>/p>
  15. <</span>/body>
  16. <</span>/html>
转自: http://blog.chinaunix.net/uid-1838361-id-3271391.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值