search
HomeDatabaseMysql Tutorialnginx+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

  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 \
  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">
  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>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor