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_keyyour_secret_key为你的reCAPTCHA API密钥。

依赖库

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

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

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

相关推荐
艺杯羹10 天前
Maven 之 JUnit 测试体系构建全解析
java·junit·maven
转码的小石12 天前
深入Java大厂面试:从Spring框架到微服务架构的技术解析
java·spring boot·spring cloud·微服务·junit·spring security·hibernate
专注代码七年13 天前
Spring Boot单元测试终极指南:从环境搭建到分层测试实战
java·spring boot·junit
xiaoxi66615 天前
用好 JUnit 5 的高级特性:提升单测效率和质量
junit
伊成20 天前
保姆级安装OpenResty教程
openresty
什么都想学的阿超1 个月前
【Redis系列 03】掌握Redis编程艺术:事务、管道与Lua脚本完全指南
redis·junit·lua
爱瑞瑞1 个月前
Junit5测试类编写笔记
junit
黄暄1 个月前
分布式锁优化:使用Lua脚本保证释放锁的原子性问题
java·redis·分布式·后端·junit·lua
Clownseven1 个月前
Nginx Lua模块(OpenResty)实战:动态化、智能化你的Nginx,实现复杂Web逻辑 (2025)
nginx·lua·openresty
cxh_陈1 个月前
org.junit.runners.model.InvalidTestClassError:此类问题的解决
java·开发语言·junit