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,实现了一个完整的人机验证流程,并在验证通过后重定向回原始页面。

相关推荐
emo了小猫16 小时前
Redis 执行 Lua 脚本过程中报错,会发生什么
redis·junit·lua
SoleMotive.3 天前
redis实现漏桶算法--https://blog.csdn.net/m0_74908430/article/details/155076710
redis·算法·junit
虹科网络安全5 天前
艾体宝干货 | Redis Python 开发系列#4 保证原子性与性能
redis·python·junit
我发在否5 天前
OpenResty > 平滑升级:1.25.x → 1.27.x
junit·openresty
IMPYLH5 天前
Lua 的 pairs 函数
开发语言·笔记·后端·junit·单元测试·lua
安冬的码畜日常5 天前
【JUnit实战3_35】第二十二章:用 JUnit 5 实现测试金字塔策略
测试工具·junit·单元测试·集成测试·系统测试·bdd·测试金字塔
IMPYLH8 天前
Lua 的 collectgarbage 函数
开发语言·笔记·junit·单元测试·lua
IMPYLH9 天前
Lua 的 assert 函数
开发语言·笔记·junit·单元测试·lua
倚肆11 天前
Spring Boot 日志系统全面详解
spring boot·junit·单元测试
百***489312 天前
Nginx实现接口复制
运维·nginx·junit