• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

nginx+lua实现登陆验证

mysql 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

用于在多台服务器上单点登录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

  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)

参考:https://github.com/LuaLanes/lanes/issues/18

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 \ 本文来源[email protected]搞@^&代*@码)网5
  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。


详细信息查看:http://wiki.nginx.org/HttpLuaModule#ngx.req.set_header

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=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

  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. <html xmlns=http://www.w3.org/1999/xhtml&#8221;>
  2. <head>
  3. <meta http-equiv=“Content-Type” content=“text/html; charset=gbk”>
  4. </head>
  5. <body>
  6. <p>
  7. <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. </script>
  14. </p>
  15. </body>
  16. </html>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:nginx+lua实现登陆验证

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址