Openresty人机验证流程

Openresty在访问正常页面的时候发现需要人机验证,然后跳人机验证页面,在完成人机验证后怎么跳转到正常的页面呢,整个流程。

在OpenResty中实现人机验证机制并在完成人机验证后跳转到正常页面,可以通过使用Nginx的配置、Lua代码以及reCAPTCHA等来完成。以下是一个完整的流程和代码示例:

流程概述

  1. 用户访问正常页面。
  2. 检查用户是否需要进行人机验证。
  3. 如果需要,跳转到人机验证页面。
  4. 用户在验证页面进行人机验证。
  5. 验证成功后,重定向回原始页面。

示例代码

1. 安装OpenResty和必要的依赖

确保你已经安装了OpenResty,并安装了lua-resty-http库用于处理HTTP请求。

2. Nginx 配置文件

更新你的Nginx配置文件(例如nginx.conf)以包含Lua代码和处理逻辑。

nginx 复制代码
http {
    lua_shared_dict captcha_cache 10m;

    server {
        listen 80;

        # 普通页面
        location / {
            access_by_lua_block {
                local uri = ngx.var.request_uri
                local captcha_cache = ngx.shared.captcha_cache
                local captcha_verified = captcha_cache:get(ngx.var.remote_addr)

                if not captcha_verified then
                    -- 未验证,重定向到人机验证页面
                    return ngx.redirect("/captcha?redirect_to=" .. uri)
                end
            }

            # 正常处理请求
            content_by_lua_block {
                ngx.say("Welcome to the protected page!")
            }
        }

        # 人机验证页面
        location /captcha {
            content_by_lua_block {
                local args = ngx.req.get_uri_args()
                local redirect_to = args.redirect_to or "/"

                ngx.say([[
                    <html>
                    <head>
                        <title>CAPTCHA Verification</title>
                        <script src="https://www.google.com/recaptcha/api.js" async defer></script>
                    </head>
                    <body>
                        <form action="/verify_captcha" method="POST">
                            <div class="g-recaptcha" data-sitekey="your_site_key"></div>
                            <br/>
                            <input type="hidden" name="redirect_to" value="]] .. redirect_to .. [[">
                            <input type="submit" value="Submit">
                        </form>
                    </body>
                    </html>
                ]])
            }
        }

        # 验证人机验证结果
        location /verify_captcha {
            content_by_lua_block {
                local http = require "resty.http"
                local req = http.new()

                ngx.req.read_body()
                local post_args = ngx.req.get_post_args()
                local recaptcha_response = post_args["g-recaptcha-response"]
                local redirect_to = post_args["redirect_to"] or "/"

                local res, err = req:request_uri("https://www.google.com/recaptcha/api/siteverify", {
                    method = "POST",
                    body = "secret=your_secret_key&response=" .. recaptcha_response,
                    headers = {
                        ["Content-Type"] = "application/x-www-form-urlencoded",
                    },
                })

                if not res then
                    ngx.say("Failed to verify captcha: ", err)
                    return
                end

                local cjson = require "cjson"
                local result = cjson.decode(res.body)

                if result.success then
                    local captcha_cache = ngx.shared.captcha_cache
                    captcha_cache:set(ngx.var.remote_addr, true, 3600) -- 验证通过,缓存1小时
                    return ngx.redirect(redirect_to)
                else
                    ngx.say("Captcha verification failed. Please try again.")
                end
            }
        }
    }
}

解释

  1. 检查验证码状态 :在普通页面请求时,通过access_by_lua_block检查用户是否已经通过验证码验证。如果没有通过,重定向到验证码页面。

  2. 显示验证码页面 :在/captcha路径中,通过Lua生成一个包含reCAPTCHA的表单页面。

  3. 处理验证码验证 :在/verify_captcha路径中,通过Lua处理验证码表单的提交。使用lua-resty-http库向Google reCAPTCHA API发送请求,验证用户提交的验证码。

  4. 缓存验证结果 :如果验证通过,将用户的IP地址存储在共享字典captcha_cache中,缓存1小时。然后重定向回用户原来的请求页面。

  5. reCAPTCHA集成 :在验证码页面中,集成了Google reCAPTCHA。需要替换your_site_key和your_secret_key为你的reCAPTCHA API密钥。

依赖库

确保你已经安装了lua-resty-http库。如果没有,可以通过OpenResty的opm工具进行安装:

sh 复制代码
opm get pintsized/lua-resty-http

这个方案通过使用Nginx的配置和Lua脚本,结合Google reCAPTCHA,实现了一个完整的人机验证流程,并在验证通过后重定向回原始页面。

相关推荐
寂寞的船3 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
努力努力再努力wz6 天前
【Lua 进阶系列】从 C++ 对象到 Lua 变量:一文理解 userdata、Lua 栈与元表绑定机制
c++·junit·lua
RobinDevNotes7 天前
从 Electron 到 GPUI,9 个跨平台桌面方案该怎么选
前端·javascript·rust·electron·openresty·前端开发工具
努力努力再努力wz10 天前
【Lua进阶系列】Lua 调试机制详解:从虚拟机调用栈、错误传播到 debug 库
junit·单元测试·lua
BuHuaX14 天前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua
RobinDevNotes14 天前
Memvid 把 Agent 长期记忆塞进一个文件
人工智能·rust·openresty
海棠Flower未眠14 天前
下单请求处理流程:商品维度分布式锁与 Redis 预扣库存
redis·分布式·junit
努力努力再努力wz14 天前
【Lua 入门系列】模块与包(一):从多文件组织到require 运行时加载机制、模块缓存、预加载搜索与 Lua 文件搜索机制
开发语言·junit·lua
CarIise16 天前
Spring 框架:多配置文件、开发方式与 IOC 注解
junit·maven·jar
努力努力再努力wz16 天前
【Lua入门系列】从 Table 到元表与元方法:一文理解 Lua 的行为扩展机制
开发语言·junit·lua